A Criteria Object contains the information used to retrieve your desired query results.
Criteria Objects are passed as parameters in the methods that perform CRUD operations, like .find() and .destroy() .
Criteria objects are composed of Query Pairs. This is what they look like.
The key is a model attribute and the value is what you want to search for.
like this
// `Model Attribute Name` : 'Some Value'
name : 'walter'
Modified pairs also have model attributes for keys but they also use Query Modifiers to perform additional useful operations.
like this
/*
`Model Attribute Name` : {
' `Query Modifier` ' : 'Some Value'}
}
*/
name : {
'contains' : 'alt' }
These work similarly to mysql 'in queries'. Each element in the array is treated as 'or'.
like this
// `Model Attribute Name` : ['Some Value', 'Some Value']
name : ['Walter','Skyler']
These can contain any number of normal or modified 'query pair's returning records when any of them are matched.
like this
// or : [`normalPair`, `modifiedPair', `normalPair`]
or : ['Skyler',{ name : {
'contains' : 'alt'} },'Flynn']
- Criteria Objects can contain any number of 'Normal', 'Modified', or 'In' Query Pairs but only 1 'Or' Query Pair.
- Each pair must match a record otherwise that record isn't returned. Pairs are treated as 'and' in your query.
- All attribute values searched for are case INsensitive. You can't find Mike without also getting mikE.
I want records where 'name' is 'Walter' and 'age' is 45.
var myCriteria = {
name: 'walter',
age: 45
}
I want records where 'name' contains 'yle' if they have 'age' 40.
var myCriteria = {
name : { 'contains' : 'yle' },
age: 40
}
I want records where 'name' is 'Walter' or 'profession' is 'cook' but only if their 'age' is at least 45.
var myCriteria = {
or : [{'name':'walter'},{'profession':'cook'}],
age: {'>=':45}
}
I want records where 'name' is 'Walter' or 'jessie'. They must be a 'cook' and their 'age' must be either 45 or 28.
var myCriteria = {
name: ['walter','jessie'],
profession: 'cook',
age: {'>=':45}
}
I want records where either the 'name' is 'walter' or 'jessie', their 'profession' is 'cook', and they are at least 35
or
I want records where the 'name' is 'skyler' and their 'profession' is 'accountant' or 'car wash professional'.
var myCriteria = {
or: [{
name:['walter','jessie'],
profession:'cook',
age:{'>=':35}
},{
name:'skyler',
profession:['accountant','car wash professional']
}]
}
The query modifiers may not work on the 'id' attribute with certain adapters (like mongo) because of the way they are treated by their respective databases. You cannot use more than one query modifier in a 'modified' Query Pair . This restriction might be lifted in future versions.