Class | Mongoid::Criteria |
In: |
lib/mongoid/criteria.rb
|
Parent: | Object |
The Criteria class is the core object needed in Mongoid to retrieve objects from the database. It is a DSL that essentially sets up the selector and options arguments that get passed on to a Mongo::Collection in the Ruby driver. Each method on the Criteria returns self to they can be chained in order to create a readable criterion to be executed against the database.
@example Create and execute a criteria.
criteria = Criteria.new criteria.only(:field).where(:field => "value").skip(20).limit(20) criteria.execute
to_a | -> | to_ary |
documents | [RW] | |
embedded | [RW] | |
field_list | [RW] | |
ids | [RW] | |
klass | [RW] | |
options | [RW] | |
selector | [RW] |
Create the new Criteria object. This will initialize the selector and options hashes, as well as the type of criteria.
@example Instantiate a new criteria.
Criteria.new(Model, true)
@param [ Class ] klass The model the criteria is for. @param [ true, false ] embedded Is the criteria for embedded docs.
Concatinate the criteria with another enumerable. If the other is a Criteria then it needs to get the collection from it.
@example Concat 2 criteria.
criteria + criteria
@param [ Criteria ] other The other criteria.
Returns the difference between the criteria and another enumerable. If the other is a Criteria then it needs to get the collection from it.
@example Get the difference of 2 criteria.
criteria - criteria
@param [ Criteria ] other The other criteria.
Returns true if the supplied Enumerable or Criteria is equal to the results of this Criteria or the criteria itself.
@note This will force a database load when called if an enumerable is passed.
@param [ Object ] other The other Enumerable or Criteria to compare to.
@return [ true, false ] If the objects are equal.
Get the collection associated with the criteria.
@example Get the collection.
criteria.collection
@return [ Collection ] The collection.
@since 2.2.0
Return or create the context in which this criteria should be executed.
This will return an Enumerable context if the class is embedded, otherwise it will return a Mongo context for root classes.
@example Get the appropriate context.
criteria.context
@return [ Mongo, Enumerable ] The appropriate context.
Return true if the criteria has some Document or not.
@example Are there any documents for the criteria?
criteria.exists?
@return [ true, false ] If documents match.
Extract a single id from the provided criteria. Could be in an $and query or a straight _id query.
@example Extract the id.
criteria.extract_id
@return [ Object ] The id.
@since 2.3.0
Merges another object with this Criteria and returns a new criteria. The other object may be a Criteria or a Hash. This is used to combine multiple scopes together, where a chained scope situation may be desired.
@example Merge the criteria with a conditions hash.
criteria.merge({ :conditions => { :title => "Sir" } })
@example Merge the criteria with another criteria.
criteri.merge(other_criteria)
@param [ Criteria, Hash ] other The other criterion to merge with.
@return [ Criteria ] A cloned self.
Convenience method of raising an invalid options error.
@example Raise the error.
criteria.raise_invalid
@raise [ Errors::InvalidOptions ] The error.
@since 2.0.0
Returns true if criteria responds to the given method.
@example Does the criteria respond to the method?
crtiteria.respond_to?(:each)
@param [ Symbol ] name The name of the class method on the Document. @param [ true, false ] include_private Whether to include privates.
@return [ true, false ] If the criteria responds to the method.
Search for documents based on a variety of args.
@example Find by an id.
criteria.search(BSON::ObjectId.new)
@example Find by multiple ids.
criteria.search([ BSON::ObjectId.new, BSON::ObjectId.new ])
@example Conditionally find all matching documents.
criteria.search(:all, :conditions => { :title => "Sir" })
@example Conditionally find the first document.
criteria.search(:first, :conditions => { :title => "Sir" })
@example Conditionally find the last document.
criteria.search(:last, :conditions => { :title => "Sir" })
@param [ Symbol, BSON::ObjectId, Array<BSON::ObjectId> ] arg The
argument to search with.
@param [ Hash ] options The options to search with.
@return [ Array<Symbol, Criteria> ] The type and criteria.
@since 2.0.0
Return the entries of the other criteria or the object. Used for comparing criteria or an enumerable.
@example Get the comparable version.
criteria.comparable(other)
@param [ Criteria ] other Another criteria.
@return [ Array ] The array to compare with.
Get the raw driver collection from the criteria.
@example Get the raw driver collection.
criteria.driver
@return [ Mongo::Collection ] The driver collection.
@since 2.2.0
Update the selector setting the operator on the value for each key in the supplied attributes Hash.
@example Update the selector.
criteria.update_selector({ :field => "value" }, "$in")
@param [ Hash, Array ] attributes The values to convert and apply. @param [ String ] operator The MongoDB operator. @param [ Symbol ] combine The operator to use when combining sets.