Class: Mongoid::Contexts::Mongo
Instance Attribute Summary collapse
-
#criteria ⇒ Object
Returns the value of attribute criteria.
Instance Method Summary collapse
-
#add_to_set(field, value) ⇒ Object
Perform an add to set on the matching documents.
-
#aggregate ⇒ Hash
Aggregate the context.
-
#avg(field) ⇒ Numeric
Get the average value for the supplied field.
-
#blank? ⇒ true, false
(also: #empty?)
Determine if the context is empty or blank given the criteria.
-
#count(extras = false) ⇒ Integer
(also: #size, #length)
Get the count of matching documents in the database for the context.
-
#delete_all ⇒ Integer
(also: #delete)
Delete all the documents in the database matching the selector.
-
#destroy_all ⇒ Integer
(also: #destroy)
Destroy all the documents in the database matching the selector.
-
#distinct(field) ⇒ Array<Object>
Gets an array of distinct values for the supplied field across the entire collection or the susbset given the criteria.
-
#execute ⇒ Cursor
Execute the context.
-
#first ⇒ Document
(also: #one)
Return the first result for the
Context
. -
#group ⇒ Hash
Groups the context.
-
#initialize(criteria) ⇒ Mongo
constructor
Create the new mongo context.
-
#iterate(&block) ⇒ Object
Iterate over each
Document
in the results. -
#last ⇒ Document
Return the last result for the
Context
. -
#load_ids(key) ⇒ Array<String, BSON::ObjectId>
Loads an array of ids only for the current criteria.
-
#max(field) ⇒ Numeric
Return the max value for a field.
-
#min(field) ⇒ Numeric
Return the min value for a field.
-
#pull(field, value) ⇒ Object
Perform a pull on the matching documents.
-
#shift ⇒ Document
Return the first result for the
Context
and skip it for successive calls. -
#sum(field) ⇒ Numeric
Sum the context.
-
#update_all(attributes = {}) ⇒ Object
(also: #update)
Very basic update that will perform a simple atomic $set of the attributes provided in the hash.
Constructor Details
#initialize(criteria) ⇒ Mongo
Create the new mongo context. This will execute the queries given the selector and options against the database.
226 227 228 229 230 231 232 |
# File 'lib/mongoid/contexts/mongo.rb', line 226 def initialize(criteria) @criteria = criteria if klass.hereditary? && !criteria.selector.keys.include?(:_type) @criteria = criteria.in(:_type => criteria.klass._types) end @criteria.cache if klass.cached? end |
Instance Attribute Details
#criteria ⇒ Object
Returns the value of attribute criteria.
5 6 7 |
# File 'lib/mongoid/contexts/mongo.rb', line 5 def criteria @criteria end |
Instance Method Details
#add_to_set(field, value) ⇒ Object
Perform an add to set on the matching documents.
21 22 23 24 25 26 27 |
# File 'lib/mongoid/contexts/mongo.rb', line 21 def add_to_set(field, value) klass.collection.update( selector, { "$addToSet" => { field => value } }, :multi => true ) end |
#aggregate ⇒ Hash
Aggregate the context. This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with counts.
38 39 40 41 42 43 44 45 |
# File 'lib/mongoid/contexts/mongo.rb', line 38 def aggregate klass.collection.group( :key => field_list, :cond => selector, :initial => { :count => 0 }, :reduce => Javascript.aggregate ) end |
#avg(field) ⇒ Numeric
Get the average value for the supplied field.
This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with averages.
60 61 62 63 |
# File 'lib/mongoid/contexts/mongo.rb', line 60 def avg(field) total = sum(field) total ? (total / count) : nil end |
#blank? ⇒ true, false Also known as: empty?
Determine if the context is empty or blank given the criteria. Will perform a quick has_one asking only for the id.
72 73 74 |
# File 'lib/mongoid/contexts/mongo.rb', line 72 def blank? klass.collection.find_one(selector, { :fields => [ :_id ] }).nil? end |
#count(extras = false) ⇒ Integer Also known as: size, length
Get the count of matching documents in the database for the context.
89 90 91 92 93 94 95 |
# File 'lib/mongoid/contexts/mongo.rb', line 89 def count(extras = false) if cached? @count ||= collection.find(selector, ).count(extras) else collection.find(selector, ).count(extras) end end |
#delete_all ⇒ Integer Also known as: delete
Delete all the documents in the database matching the selector.
107 108 109 |
# File 'lib/mongoid/contexts/mongo.rb', line 107 def delete_all klass.delete_all(:conditions => selector) end |
#destroy_all ⇒ Integer Also known as: destroy
Destroy all the documents in the database matching the selector.
120 121 122 |
# File 'lib/mongoid/contexts/mongo.rb', line 120 def destroy_all klass.destroy_all(:conditions => selector) end |
#distinct(field) ⇒ Array<Object>
Gets an array of distinct values for the supplied field across the entire collection or the susbset given the criteria.
134 135 136 |
# File 'lib/mongoid/contexts/mongo.rb', line 134 def distinct(field) klass.collection.distinct(field, selector) end |
#execute ⇒ Cursor
Execute the context. This will take the selector and options and pass them on to the Ruby driver’s find() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned new documents of the type of class provided will be instantiated.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mongoid/contexts/mongo.rb', line 147 def execute collection, = klass.collection, if criteria.inclusions.any? collection.find(selector, ).entries.tap do |docs| parent_ids = docs.map(&:id) criteria.inclusions.reject! do || if .macro == :referenced_in child_ids = load_ids(.foreign_key) .eager_load(child_ids) else .eager_load(parent_ids) end end end else collection.find(selector, ) end end |
#first ⇒ Document Also known as: one
Return the first result for the Context
.
190 191 192 193 |
# File 'lib/mongoid/contexts/mongo.rb', line 190 def first attributes = klass.collection.find_one(selector, ) attributes ? Mongoid::Factory.from_db(klass, attributes) : nil end |
#group ⇒ Hash
Groups the context. This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with objects.
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/mongoid/contexts/mongo.rb', line 205 def group klass.collection.group( :key => field_list, :cond => selector, :initial => { :group => [] }, :reduce => Javascript.group ).collect do |docs| docs["group"] = docs["group"].collect do |attrs| Mongoid::Factory.from_db(klass, attrs) end docs end end |
#iterate(&block) ⇒ Object
Iterate over each Document
in the results. This can take an optional block to pass to each argument in the results.
239 240 241 242 243 244 |
# File 'lib/mongoid/contexts/mongo.rb', line 239 def iterate(&block) return caching(&block) if cached? if block_given? execute.each { |doc| yield doc } end end |
#last ⇒ Document
Return the last result for the Context
. Essentially does a find_one on the collection with the sorting reversed. If no sorting parameters have been provided it will default to ids.
254 255 256 257 258 259 |
# File 'lib/mongoid/contexts/mongo.rb', line 254 def last opts = opts[:sort] = opts[:sort].map{ |option| [ option[0], option[1].invert ] }.uniq attributes = klass.collection.find_one(selector, opts) attributes ? Mongoid::Factory.from_db(klass, attributes) : nil 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.
177 178 179 180 181 182 |
# File 'lib/mongoid/contexts/mongo.rb', line 177 def load_ids(key) klass.collection.driver.find( selector, .merge({ :fields => { key => 1 }}) ).map { |doc| doc[key] } end |
#max(field) ⇒ Numeric
Return the max value for a field.
This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with sums.
274 275 276 |
# File 'lib/mongoid/contexts/mongo.rb', line 274 def max(field) grouped(:max, field.to_s, Javascript.max, Javascript.max_finalize) end |
#min(field) ⇒ Numeric
Return the min value for a field.
This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with sums.
291 292 293 |
# File 'lib/mongoid/contexts/mongo.rb', line 291 def min(field) grouped(:min, field.to_s, Javascript.min, Javascript.min_finalize) end |
#pull(field, value) ⇒ Object
Perform a pull on the matching documents.
306 307 308 309 310 311 312 |
# File 'lib/mongoid/contexts/mongo.rb', line 306 def pull(field, value) klass.collection.update( selector, { "$pull" => { field => value } }, :multi => true ) end |
#shift ⇒ Document
Return the first result for the Context
and skip it for successive calls.
321 322 323 |
# File 'lib/mongoid/contexts/mongo.rb', line 321 def shift first.tap { criteria.skip(([:skip] || 0) + 1) } end |
#sum(field) ⇒ Numeric
Sum the context.
This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with sums.
338 339 340 |
# File 'lib/mongoid/contexts/mongo.rb', line 338 def sum(field) grouped(:sum, field.to_s, Javascript.sum, Javascript.sum_finalize) end |
#update_all(attributes = {}) ⇒ Object Also known as: update
Very basic update that will perform a simple atomic $set of the attributes provided in the hash. Can be expanded to later for more robust functionality.
352 353 354 355 356 357 358 359 360 |
# File 'lib/mongoid/contexts/mongo.rb', line 352 def update_all(attributes = {}) klass.collection.update( selector, { "$set" => attributes }, Safety.(:multi => true) ).tap do Threaded. end end |