The search method retrieves an array of objects from the object store. It can optionally take one or two parameters that constrains the search and results.
Filtering
The first parameter allows you to constrain the results to only instances matching the search terms. By omitting the parameter all the objects in the object store are returned. This parmeter should be an object. The properties of this object describe the properties of the instances you wish to retrieve.
For example, to retrieve all instances of the class Fish where the 'colour' property is 'green' and like freshwater:
var objects = Fish.search({ 'colour':'green', 'water_type':'fresh' });
You can modify the search type by changing the properties of the search object into the form
[fieldname]__[clause]
for instance, we can search for fish with a name containing 'bass' as follows:
var objects = Fish.search({ 'name__contains':'bass' });
The clauses avaliable are:
- startswith - The value of field must start with 'value'.
- istartswith - case-insensitive version of startswith.
- endswith - The value of the field must end with 'value'.
- iendswith - case-insensitive version of iendswith
- contains - matches if any part of the value of 'fieldname' contains 'value'
- exact - searches for an exact match of 'value' within fieldname.
- iexact - case-insenstive version of exact.
- gt - matches all results that are > 'value' (sorting alphabetically)
- lt - as with gt, except that the matches must be < 'value'.
- in - matches all instances which exactly match one item in list of terms, (eg. 'b', 'c')
- not - to find instances where an attribute is not some value.
- gte - to find values greater than or equal to the passed value
- lte - to find values less than or equal to the passed value
You may combine any number of property tests with any clause.
Ordering
The second parameter to the search function determines the way the results are presented. The three keys allowed here are 'orderby', 'page' and 'perpage':
- per_page - The number of results to return in a given paged result set
- page - Which page of results to return
- order_by - Orders the results by a particular property
The 'order_by' property is the most powerful. It should be the name of one of the properties of the objects to sort by a single property, or a list of property names, to sort by more than one. The ordering can be affected by two other parameters - firstly, placing a '-' at the beginning of the term will reverse the sort order. Secondly, you can affect the sorting method by placing ':numeric' at the end of the term, to treat the value as a number when sorting.
For example, so search for all 'Person' instances created after January 1st, 2006, with a name starting with 'joe', sorting firstly by age numerically, and by reverse creation date where the ages are the same, returning the second result set of 10:
Person.search({
'creation_date__gt': new Date(2006,0,1),
'name__istartswith': 'Joe'
},{
'order_by': ['name','age:numeric','-creation_date'],
'page': 2,
'per_page': 10
});