Class: Mongoid::Criteria
- Inherits:
-
Object
- Object
- Mongoid::Criteria
- Includes:
- Enumerable, Contextual, Mongoid::Criterion::Inspection, Mongoid::Criterion::Scoping, Origin::Queryable
- Defined in:
- lib/mongoid/criteria.rb
Overview
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.
Instance Attribute Summary collapse
-
#embedded ⇒ Object
Returns the value of attribute embedded.
-
#klass ⇒ Object
Returns the value of attribute klass.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Returns true if the supplied
Enumerable
orCriteria
is equal to the results of thisCriteria
or the criteria itself. -
#as_json(options = nil) ⇒ String
Needed to properly get a criteria back as json.
-
#build(attrs = {}) ⇒ Document
(also: #new)
Build a document given the selector and return it.
-
#cache ⇒ Criteria
Tells the criteria that the cursor that gets returned needs to be cached.
-
#cached? ⇒ true, false
Will return true if the cache option has been set.
-
#create(attrs = {}) ⇒ Document
Create a document in the database given the selector and return it.
-
#create!(attrs = {}) ⇒ Document
Create a document in the database given the selector and return it.
-
#documents ⇒ Array<Document>
Get the documents from the embedded criteria.
-
#documents=(docs) ⇒ Array<Document>
Set the embedded documents on the criteria.
-
#embedded? ⇒ true, false
Is the criteria for embedded documents?.
-
#execute_or_raise(ids, multi) ⇒ Document+
Execute the criteria or raise an error if no documents found.
-
#exists? ⇒ true, false
Return true if the criteria has some Document or not.
-
#extract_id ⇒ Object
Extract a single id from the provided criteria.
-
#extras(extras) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies additional options to be passed to the Ruby driver, in the exact format for the driver. -
#field_list ⇒ Array<String>
Get the list of included fields.
-
#find(*args) ⇒ Array<Document>, Document
Find the matchind document(s) in the criteria for the provided ids.
-
#for_ids(ids) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies an id that must be matched. -
#freeze ⇒ Criteria
When freezing a criteria we need to initialize the context first otherwise the setting of the context on attempted iteration will raise a runtime error.
-
#from_map_or_db ⇒ Document
Get the document from the identity map, and if not found hit the database.
-
#includes(*relations) ⇒ Criteria
Eager loads all the provided relations.
-
#inclusions ⇒ Array<Metadata>
Get a list of criteria that are to be executed for eager loading.
-
#inclusions=(value) ⇒ Array<Metadata>
Set the inclusions for the criteria.
-
#initialize(klass) ⇒ Criteria
constructor
Initialize the new criteria.
-
#merge(other) ⇒ Criteria
Merges another object with this
Criteria
and returns a new criteria. -
#merge!(other) ⇒ Criteria
Merge the other criteria into this one.
-
#multiple_from_map_or_db(ids) ⇒ Array<Document>
Get the documents from the identity map, and if not found hit the database.
-
#only(*args) ⇒ Criteria
Overriden to include _type in the fields.
-
#respond_to?(name, include_private = false) ⇒ true, false
Returns true if criteria responds to the given method.
-
#to_criteria ⇒ Criteria
Convenience for objects that want to be merged into a criteria.
-
#to_proc ⇒ Proc
Convert the criteria to a proc.
-
#type(types) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies a type or an Array of types that must be matched. -
#where(expression) ⇒ Criteria
This is the general entry point for most MongoDB queries.
Methods included from Mongoid::Criterion::Scoping
#apply_default_scope, #remove_scoping, #scoped, #scoped?, #scoping_options, #scoping_options=, #unscoped, #unscoped?, #with_default_scope
Methods included from Mongoid::Criterion::Inspection
Methods included from Contextual
Constructor Details
#initialize(klass) ⇒ Criteria
Initialize the new criteria.
333 334 335 336 |
# File 'lib/mongoid/criteria.rb', line 333 def initialize(klass) @klass = klass super(klass.aliased_fields, klass.fields) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
Used for chaining Criteria
scopes together in the for of class methods on the Document
the criteria is for.
653 654 655 656 657 658 659 660 661 |
# File 'lib/mongoid/criteria.rb', line 653 def method_missing(name, *args, &block) if klass.respond_to?(name) klass.send(:with_scope, self) do klass.send(name, *args, &block) end else return entries.send(name, *args) end end |
Instance Attribute Details
#embedded ⇒ Object
Returns the value of attribute embedded.
20 21 22 |
# File 'lib/mongoid/criteria.rb', line 20 def @embedded end |
#klass ⇒ Object
Returns the value of attribute klass.
20 21 22 |
# File 'lib/mongoid/criteria.rb', line 20 def klass @klass end |
Instance Method Details
#==(other) ⇒ true, false
This will force a database load when called if an enumerable is passed.
Returns true if the supplied Enumerable
or Criteria
is equal to the results of this Criteria
or the criteria itself.
32 33 34 35 |
# File 'lib/mongoid/criteria.rb', line 32 def ==(other) return super if other.respond_to?(:selector) entries == other end |
#as_json(options = nil) ⇒ String
Needed to properly get a criteria back as json
45 46 47 |
# File 'lib/mongoid/criteria.rb', line 45 def as_json( = nil) entries.as_json() end |
#build(attrs = {}) ⇒ Document Also known as: new
Build a document given the selector and return it. Complex criteria, such as $in and $or operations will get ignored.
61 62 63 |
# File 'lib/mongoid/criteria.rb', line 61 def build(attrs = {}) create_document(:new, attrs) end |
#cache ⇒ Criteria
Tells the criteria that the cursor that gets returned needs to be cached. This is so multiple iterations don’t hit the database multiple times, however this is not advisable when working with large data sets as the entire results will get stored in memory.
75 76 77 78 79 |
# File 'lib/mongoid/criteria.rb', line 75 def cache crit = clone crit..merge!(cache: true) crit end |
#cached? ⇒ true, false
Will return true if the cache option has been set.
87 88 89 |
# File 'lib/mongoid/criteria.rb', line 87 def cached? [:cache] == true end |
#create(attrs = {}) ⇒ Document
Create a document in the database given the selector and return it. Complex criteria, such as $in and $or operations will get ignored.
103 104 105 |
# File 'lib/mongoid/criteria.rb', line 103 def create(attrs = {}) create_document(:create, attrs) end |
#create!(attrs = {}) ⇒ Document
Create a document in the database given the selector and return it. Complex criteria, such as $in and $or operations will get ignored. If validation fails, an error will be raised.
122 123 124 |
# File 'lib/mongoid/criteria.rb', line 122 def create!(attrs = {}) create_document(:create!, attrs) end |
#documents ⇒ Array<Document>
Get the documents from the embedded criteria.
134 135 136 |
# File 'lib/mongoid/criteria.rb', line 134 def documents @documents ||= [] end |
#documents=(docs) ⇒ Array<Document>
Set the embedded documents on the criteria.
147 148 149 |
# File 'lib/mongoid/criteria.rb', line 147 def documents=(docs) @documents = docs end |
#embedded? ⇒ true, false
Is the criteria for embedded documents?
159 160 161 |
# File 'lib/mongoid/criteria.rb', line 159 def !!@embedded end |
#execute_or_raise(ids, multi) ⇒ Document+
Execute the criteria or raise an error if no documents found.
175 176 177 178 179 |
# File 'lib/mongoid/criteria.rb', line 175 def execute_or_raise(ids, multi) result = multiple_from_map_or_db(ids) check_for_missing_documents!(result, ids) multi ? result : result.first end |
#exists? ⇒ true, false
Return true if the criteria has some Document or not.
189 190 191 |
# File 'lib/mongoid/criteria.rb', line 189 def exists? context.count > 0 end |
#extract_id ⇒ Object
Extract a single id from the provided criteria. Could be in an $and query or a straight _id query.
202 203 204 |
# File 'lib/mongoid/criteria.rb', line 202 def extract_id selector.extract_id end |
#extras(extras) ⇒ Criteria
Adds a criterion to the Criteria
that specifies additional options to be passed to the Ruby driver, in the exact format for the driver.
criteria.extras(:limit => 20, :skip => 40)
217 218 219 220 221 |
# File 'lib/mongoid/criteria.rb', line 217 def extras(extras) crit = clone crit..merge!(extras) crit end |
#field_list ⇒ Array<String>
Get the list of included fields.
231 232 233 234 235 236 237 |
# File 'lib/mongoid/criteria.rb', line 231 def field_list if [:fields] [:fields].keys.reject{ |key| key == "_type" } else [] end end |
#find(*args) ⇒ Array<Document>, Document
Find the matchind document(s) in the criteria for the provided ids.
252 253 254 255 256 |
# File 'lib/mongoid/criteria.rb', line 252 def find(*args) ids = args.__find_args__ raise_invalid if ids.any?(&:nil?) for_ids(ids).execute_or_raise(ids, args.multi_arged?) end |
#for_ids(ids) ⇒ Criteria
Adds a criterion to the Criteria
that specifies an id that must be matched.
269 270 271 272 273 274 275 276 277 |
# File 'lib/mongoid/criteria.rb', line 269 def for_ids(ids) ids = mongoize_ids(ids) method = extract_id ? :all_of : :where if ids.size > 1 send(method, { _id: { "$in" => ids }}) else send(method, { _id: ids.first }) end end |
#freeze ⇒ Criteria
When freezing a criteria we need to initialize the context first otherwise the setting of the context on attempted iteration will raise a runtime error.
289 290 291 |
# File 'lib/mongoid/criteria.rb', line 289 def freeze context and inclusions and super end |
#from_map_or_db ⇒ Document
Get the document from the identity map, and if not found hit the database.
302 303 304 305 306 307 |
# File 'lib/mongoid/criteria.rb', line 302 def from_map_or_db id = extract_id id = klass.fields["_id"].mongoize(id) if id doc = IdentityMap.get(klass, id || selector.except("_type")) doc && doc.matches?(selector) ? doc : first end |
#includes(*relations) ⇒ Criteria
This will only work if Mongoid’s identity map is enabled. To do so set identity_map_enabled: true in your mongoid.yml
This will work for embedded relations that reference another collection via belongs_to as well.
Eager loading brings all the documents into memory, so there is a sweet spot on the performance gains. Internal benchmarks show that eager loading becomes slower around 100k documents, but this will naturally depend on the specific application.
Eager loads all the provided relations. Will load all the documents into the identity map who’s ids match based on the extra query for the ids.
362 363 364 365 366 367 368 |
# File 'lib/mongoid/criteria.rb', line 362 def includes(*relations) relations.each do |name| = klass.reflect_on_association(name) inclusions.push() unless inclusions.include?() end clone end |
#inclusions ⇒ Array<Metadata>
Get a list of criteria that are to be executed for eager loading.
378 379 380 |
# File 'lib/mongoid/criteria.rb', line 378 def inclusions @inclusions ||= [] end |
#inclusions=(value) ⇒ Array<Metadata>
Set the inclusions for the criteria.
392 393 394 |
# File 'lib/mongoid/criteria.rb', line 392 def inclusions=(value) @inclusions = value end |
#merge(other) ⇒ Criteria
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.
407 408 409 410 411 |
# File 'lib/mongoid/criteria.rb', line 407 def merge(other) crit = clone crit.merge!(other) crit end |
#merge!(other) ⇒ Criteria
Merge the other criteria into this one.
423 424 425 426 427 428 429 430 431 |
# File 'lib/mongoid/criteria.rb', line 423 def merge!(other) criteria = other.to_criteria selector.merge!(criteria.selector) .merge!(criteria.) self.documents = criteria.documents.dup unless criteria.documents.empty? self. = criteria. self.inclusions = (inclusions + criteria.inclusions.dup).uniq self end |
#multiple_from_map_or_db(ids) ⇒ Array<Document>
Get the documents from the identity map, and if not found hit the database.
318 319 320 321 322 323 |
# File 'lib/mongoid/criteria.rb', line 318 def multiple_from_map_or_db(ids) return entries if ids = mongoize_ids(ids) result = from_identity_map(ids) ids.empty? ? result : result + from_database(ids) end |
#only(*args) ⇒ Criteria
Overriden to include _type in the fields.
443 444 445 446 447 448 449 450 451 |
# File 'lib/mongoid/criteria.rb', line 443 def only(*args) return clone if args.empty? args = args.flatten if klass.hereditary? super(*args.push(:_type)) else super(*args) end end |
#respond_to?(name, include_private = false) ⇒ true, false
Returns true if criteria responds to the given method.
462 463 464 |
# File 'lib/mongoid/criteria.rb', line 462 def respond_to?(name, include_private = false) super || klass.respond_to?(name) || entries.respond_to?(name, include_private) end |
#to_criteria ⇒ Criteria
Convenience for objects that want to be merged into a criteria.
476 477 478 |
# File 'lib/mongoid/criteria.rb', line 476 def to_criteria self end |
#to_proc ⇒ Proc
Convert the criteria to a proc.
488 489 490 |
# File 'lib/mongoid/criteria.rb', line 488 def to_proc ->{ self } end |
#type(types) ⇒ Criteria
Adds a criterion to the Criteria
that specifies a type or an Array of types that must be matched.
502 503 504 |
# File 'lib/mongoid/criteria.rb', line 502 def type(types) any_in(_type: Array(types)) end |
#where(expression) ⇒ Criteria
This is the general entry point for most MongoDB queries. This either creates a standard field: value selection, and expanded selection with the use of hash methods, or a $where selection if a string is provided.
524 525 526 527 528 529 |
# File 'lib/mongoid/criteria.rb', line 524 def where(expression) if expression.is_a?(::String) && raise Errors::UnsupportedJavascript.new(klass, expression) end super end |