Module: Mongoid::Criterion::Inclusion
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criterion/inclusion.rb
Instance Method Summary collapse
-
#all(attributes = {}) ⇒ Criteria
(also: #all_in)
Adds a criterion to the
Criteria
that specifies values that must all be matched in order to return results. -
#also_in(attributes = {}) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies values where any can be matched in order to return results. -
#and(selector = nil) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies values that must be matched in order to return results. -
#any_of(*args) ⇒ Criteria
(also: #or)
Adds a criterion to the
Criteria
that specifies a set of expressions to match if any of them return true. -
#find(*args) ⇒ Document, Criteria
Find the matchind document in the criteria, either based on id or conditions.
-
#in(attributes = {}) ⇒ Criteria
(also: #any_in)
Adds a criterion to the
Criteria
that specifies values where any can be matched in order to return results. -
#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.
-
#load_ids(key) ⇒ Array<String, BSON::ObjectId>
Loads an array of ids only for the current criteria.
-
#near(attributes = {}) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies values to do geospacial searches by. -
#where(selector = nil) ⇒ Criteria
Adds a criterion to the
Criteria
that specifies values that must be matched in order to return results.
Instance Method Details
#all(attributes = {}) ⇒ Criteria Also known as: all_in
Adds a criterion to the Criteria
that specifies values that must all be matched in order to return results. Similar to an “in” clause but the underlying conditional logic is an “AND” and not an “OR”. The MongoDB conditional operator that will be used is “$all”.
18 19 20 |
# File 'lib/mongoid/criterion/inclusion.rb', line 18 def all(attributes = {}) update_selector(attributes, "$all") end |
#also_in(attributes = {}) ⇒ Criteria
Adds a criterion to the Criteria
that specifies values where any can be matched in order to return results. This is similar to an SQL “IN” clause. The MongoDB conditional operator that will be used is “$in”. Any previously matching “$in” arrays will be unioned with new arguments.
35 36 37 |
# File 'lib/mongoid/criterion/inclusion.rb', line 35 def also_in(attributes = {}) update_selector(attributes, "$in") end |
#and(selector = nil) ⇒ Criteria
Adds a criterion to the Criteria
that specifies values that must be matched in order to return results. This is similar to a SQL “WHERE” clause. This is the actual selector that will be provided to MongoDB, similar to the Javascript object that is used when performing a find() in the MongoDB console.
51 52 53 |
# File 'lib/mongoid/criterion/inclusion.rb', line 51 def and(selector = nil) where(selector) end |
#any_of(*args) ⇒ Criteria Also known as: or
Adds a criterion to the Criteria
that specifies a set of expressions to match if any of them return true. This is a $or query in MongoDB and is similar to a SQL OR. This is named #any_of and aliased “or” for readability.
66 67 68 69 70 71 72 73 |
# File 'lib/mongoid/criterion/inclusion.rb', line 66 def any_of(*args) clone.tap do |crit| criterion = @selector["$or"] || [] converted = BSON::ObjectId.convert(klass, args.flatten) = converted.collect { |hash| hash. } crit.selector["$or"] = criterion.concat() end end |
#find(*args) ⇒ Document, Criteria
Durran: DRY up duplicated code in a few places.
Find the matchind document in the criteria, either based on id or conditions.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mongoid/criterion/inclusion.rb', line 101 def find(*args) type, crit = search(*args) case type when :first then crit.one when :last then crit.last when :ids then execute_or_raise(args, crit) else crit end end |
#in(attributes = {}) ⇒ Criteria Also known as: any_in
Adds a criterion to the Criteria
that specifies values where any can be matched in order to return results. This is similar to an SQL “IN” clause. The MongoDB conditional operator that will be used is “$in”.
123 124 125 |
# File 'lib/mongoid/criterion/inclusion.rb', line 123 def in(attributes = {}) update_selector(attributes, "$in", :&) 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.
152 153 154 155 156 157 |
# File 'lib/mongoid/criterion/inclusion.rb', line 152 def includes(*relations) relations.each do |name| inclusions.push(klass.reflect_on_association(name)) end clone end |
#inclusions ⇒ Array<Metadata>
Get a list of criteria that are to be executed for eager loading.
167 168 169 |
# File 'lib/mongoid/criterion/inclusion.rb', line 167 def inclusions @inclusions ||= [] end |
#load_ids(key) ⇒ Array<String, BSON::ObjectId>
Loads an array of ids only for the current criteria. Used by eager loading to determine the documents to load.
182 183 184 |
# File 'lib/mongoid/criterion/inclusion.rb', line 182 def load_ids(key) driver.find(selector, { :fields => { key => 1 }}).map { |doc| doc[key] } end |
#near(attributes = {}) ⇒ Criteria
Adds a criterion to the Criteria
that specifies values to do geospacial searches by. The field must be indexed with the “2d” option.
195 196 197 |
# File 'lib/mongoid/criterion/inclusion.rb', line 195 def near(attributes = {}) update_selector(attributes, "$near") end |
#where(selector = nil) ⇒ Criteria
Adds a criterion to the Criteria
that specifies values that must be matched in order to return results. This is similar to a SQL “WHERE” clause. This is the actual selector that will be provided to MongoDB, similar to the Javascript object that is used when performing a find() in the MongoDB console.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/mongoid/criterion/inclusion.rb', line 211 def where(selector = nil) clone.tap do |crit| selector = case selector when String then {"$where" => selector} else BSON::ObjectId.convert(klass, selector || {}, false). end selector.each_pair do |key, value| if crit.selector.has_key?(key) && crit.selector[key].respond_to?(:merge!) && value.respond_to?(:merge!) crit.selector[key] = crit.selector[key].merge!(value) do |key, old, new| key == '$in' ? old & new : new end else crit.selector[key] = value end end end end |