Class: Mongoid::Criteria
- Inherits:
-
Object
- Object
- Mongoid::Criteria
- Includes:
- Enumerable, Mongoid::Clients::Options, Contextual, Findable, Includable, Inspectable, Marshalable, Modifiable, Scopable, Origin::Queryable
- Defined in:
- lib/mongoid/criteria.rb,
lib/mongoid/criteria/findable.rb,
lib/mongoid/criteria/scopable.rb,
lib/mongoid/criteria/includable.rb,
lib/mongoid/criteria/modifiable.rb,
lib/mongoid/criteria/permission.rb,
lib/mongoid/criteria/inspectable.rb,
lib/mongoid/criteria/marshalable.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.
Defined Under Namespace
Modules: Findable, Includable, Inspectable, Marshalable, Modifiable, Permission, Scopable
Constant Summary collapse
- CHECK =
Static array used to check with method missing - we only need to ever instantiate once.
[]
Instance Attribute Summary collapse
-
#embedded ⇒ Object
Returns the value of attribute embedded.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#parent_document ⇒ Object
Returns the value of attribute parent_document.
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.
-
#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.
-
#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?.
-
#empty_and_chainable? ⇒ true, false
Is the criteria an empty but chainable criteria?.
-
#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.
-
#for_js(javascript, scope = {}) ⇒ Criteria
Find documents by the provided javascript and scope.
-
#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.
-
#initialize(klass) ⇒ Criteria
constructor
Initialize the new criteria.
-
#merge(other) ⇒ Criteria
criteria.merge({ klass: Band, where: { name: “Depeche Mode” }, order_by: { name: 1 } }).
-
#merge!(other) ⇒ Criteria
Merge the other criteria into this one.
-
#none ⇒ Criteria
Returns a criteria that will always contain zero results and never hits the database.
-
#only(*args) ⇒ Criteria
Overriden to include _type in the fields.
-
#read(value = nil) ⇒ Criteria
Set the read preference for the criteria.
-
#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.
-
#without(*args) ⇒ Criteria
Overriden to exclude _id from the fields.
-
#without_options ⇒ Criteria
Get a version of this criteria without the options.
Methods included from Mongoid::Clients::Options
#collection_name, #mongo_client, #persistence_options, #with
Methods included from Scopable
#apply_default_scope, #remove_scoping, #scoped, #scoped?, #scoping_options, #scoping_options=, #unscoped, #unscoped?, #with_default_scope
Methods included from Modifiable
#build, #create, #create!, #create_with, #find_or_create_by, #find_or_create_by!, #find_or_initialize_by, #first_or_create, #first_or_create!, #first_or_initialize
Methods included from Marshalable
Methods included from Includable
#includes, #inclusions, #inclusions=
Methods included from Inspectable
Methods included from Findable
#execute_or_raise, #find, #for_ids, #multiple_from_db
Methods included from Contextual
Constructor Details
#initialize(klass) ⇒ Criteria
Initialize the new criteria.
194 195 196 197 |
# File 'lib/mongoid/criteria.rb', line 194 def initialize(klass) @klass = klass klass ? super(klass.aliased_fields, klass.fields) : super({}, {}) 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.
494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/mongoid/criteria.rb', line 494 def method_missing(name, *args, &block) if klass.respond_to?(name) klass.send(:with_scope, self) do klass.send(name, *args, &block) end elsif CHECK.respond_to?(name) return entries.send(name, *args, &block) else super end end |
Instance Attribute Details
#embedded ⇒ Object
Returns the value of attribute embedded.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def @embedded end |
#klass ⇒ Object
Returns the value of attribute klass.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def klass @klass end |
#metadata ⇒ Object
Returns the value of attribute metadata.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def @metadata end |
#parent_document ⇒ Object
Returns the value of attribute parent_document.
35 36 37 |
# File 'lib/mongoid/criteria.rb', line 35 def parent_document @parent_document 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.
47 48 49 50 |
# File 'lib/mongoid/criteria.rb', line 47 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
60 61 62 |
# File 'lib/mongoid/criteria.rb', line 60 def as_json( = nil) entries.as_json() 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.
73 74 75 76 77 |
# File 'lib/mongoid/criteria.rb', line 73 def cache crit = clone crit..merge!(cache: true) crit end |
#cached? ⇒ true, false
Will return true if the cache option has been set.
85 86 87 |
# File 'lib/mongoid/criteria.rb', line 85 def cached? [:cache] == true end |
#documents ⇒ Array<Document>
Get the documents from the embedded criteria.
97 98 99 |
# File 'lib/mongoid/criteria.rb', line 97 def documents @documents ||= [] end |
#documents=(docs) ⇒ Array<Document>
Set the embedded documents on the criteria.
110 111 112 |
# File 'lib/mongoid/criteria.rb', line 110 def documents=(docs) @documents = docs end |
#embedded? ⇒ true, false
Is the criteria for embedded documents?
122 123 124 |
# File 'lib/mongoid/criteria.rb', line 122 def !!@embedded end |
#empty_and_chainable? ⇒ true, false
Is the criteria an empty but chainable criteria?
266 267 268 |
# File 'lib/mongoid/criteria.rb', line 266 def empty_and_chainable? !!@none end |
#extract_id ⇒ Object
Extract a single id from the provided criteria. Could be in an $and query or a straight _id query.
135 136 137 |
# File 'lib/mongoid/criteria.rb', line 135 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)
150 151 152 153 154 |
# File 'lib/mongoid/criteria.rb', line 150 def extras(extras) crit = clone crit..merge!(extras) crit end |
#field_list ⇒ Array<String>
Get the list of included fields.
164 165 166 167 168 169 170 |
# File 'lib/mongoid/criteria.rb', line 164 def field_list if [:fields] [:fields].keys.reject{ |key| key == "_type" } else [] end end |
#for_js(javascript, scope = {}) ⇒ Criteria
Find documents by the provided javascript and scope. Uses a $where but is different from Criteria#where in that it will pass a code object to the query instead of a pure string. Safe against Javascript injection attacks.
431 432 433 |
# File 'lib/mongoid/criteria.rb', line 431 def for_js(javascript, scope = {}) js_query(BSON::CodeWithScope.new(javascript, scope)) 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.
182 183 184 |
# File 'lib/mongoid/criteria.rb', line 182 def freeze context and inclusions and super end |
#merge(other) ⇒ Criteria
criteria.merge({
klass: Band,
where: { name: "Depeche Mode" },
order_by: { name: 1 }
})
219 220 221 222 223 |
# File 'lib/mongoid/criteria.rb', line 219 def merge(other) crit = clone crit.merge!(other) crit end |
#merge!(other) ⇒ Criteria
Merge the other criteria into this one.
235 236 237 238 239 240 241 242 243 |
# File 'lib/mongoid/criteria.rb', line 235 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).uniq self end |
#none ⇒ Criteria
Returns a criteria that will always contain zero results and never hits the database.
254 255 256 |
# File 'lib/mongoid/criteria.rb', line 254 def none @none = true and self end |
#only(*args) ⇒ Criteria
Overriden to include _type in the fields.
280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/mongoid/criteria.rb', line 280 def only(*args) return clone if args.flatten.empty? args = args.flatten if (args & Fields::IDS).empty? args.unshift(:_id) end if klass.hereditary? super(*args.push(:_type)) else super(*args) end end |
#read(value = nil) ⇒ Criteria
Set the read preference for the criteria.
303 304 305 306 307 |
# File 'lib/mongoid/criteria.rb', line 303 def read(value = nil) clone.tap do |criteria| criteria..merge!(read: value) end end |
#respond_to?(name, include_private = false) ⇒ true, false
Returns true if criteria responds to the given method.
333 334 335 |
# File 'lib/mongoid/criteria.rb', line 333 def respond_to?(name, include_private = false) super || klass.respond_to?(name) || CHECK.respond_to?(name, include_private) end |
#to_criteria ⇒ Criteria
Convenience for objects that want to be merged into a criteria.
347 348 349 |
# File 'lib/mongoid/criteria.rb', line 347 def to_criteria self end |
#to_proc ⇒ Proc
Convert the criteria to a proc.
359 360 361 |
# File 'lib/mongoid/criteria.rb', line 359 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.
373 374 375 |
# File 'lib/mongoid/criteria.rb', line 373 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.
395 396 397 398 399 400 |
# File 'lib/mongoid/criteria.rb', line 395 def where(expression) if expression.is_a?(::String) && raise Errors::UnsupportedJavascript.new(klass, expression) end super end |
#without(*args) ⇒ Criteria
Overriden to exclude _id from the fields.
319 320 321 322 |
# File 'lib/mongoid/criteria.rb', line 319 def without(*args) args -= Fields::IDS super(*args) end |
#without_options ⇒ Criteria
Get a version of this criteria without the options.
411 412 413 414 415 |
# File 'lib/mongoid/criteria.rb', line 411 def crit = clone crit..clear crit end |