Class: Mongoid::Contexts::Mongo
Instance Attribute Summary collapse
-
#criteria ⇒ Object
Returns the value of attribute criteria.
Instance Method Summary collapse
-
#aggregate ⇒ Object
Aggregate the context.
-
#avg(field) ⇒ Object
Get the average value for the supplied field.
-
#blank? ⇒ Boolean
(also: #empty?)
Determine if the context is empty or blank given the criteria.
-
#count(extras = false) ⇒ Integer
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) ⇒ Object
Gets an array of distinct values for the supplied field across the entire collection or the susbset given the criteria.
-
#execute ⇒ Object
Execute the context.
-
#group ⇒ Object
Groups the context.
-
#grouped(start, field, reduce) ⇒ Object
Common functionality for grouping operations.
-
#initialize(criteria) ⇒ Mongo
constructor
Create the new mongo context.
-
#iterate(&block) ⇒ Object
Iterate over each
Document
in the results. -
#last ⇒ Object
Return the last result for the
Context
. -
#max(field) ⇒ Object
Return the max value for a field.
-
#min(field) ⇒ Object
Return the min value for a field.
-
#one ⇒ Object
(also: #first)
Return the first result for the
Context
. -
#process_options ⇒ Object
Filters the field list.
-
#shift ⇒ Object
Return the first result for the
Context
and skip it for successive calls. -
#sum(field) ⇒ Object
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.
Example:
Mongoid::Contexts::Mongo.new(criteria)
160 161 162 163 164 165 166 |
# File 'lib/mongoid/contexts/mongo.rb', line 160 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
#aggregate ⇒ Object
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.
Example:
context.aggregate
Returns:
A Hash
with field values as keys, counts as values
21 22 23 24 25 26 27 28 |
# File 'lib/mongoid/contexts/mongo.rb', line 21 def aggregate klass.collection.group( :key => field_list, :cond => selector, :initial => { :count => 0 }, :reduce => Javascript.aggregate ) end |
#avg(field) ⇒ Object
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.
Example:
context.avg(:age)
Returns:
A numeric value that is the average.
44 45 46 47 |
# File 'lib/mongoid/contexts/mongo.rb', line 44 def avg(field) total = sum(field) total ? (total / count) : nil end |
#blank? ⇒ Boolean 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.
Example:
context.blank?
55 56 57 |
# File 'lib/mongoid/contexts/mongo.rb', line 55 def blank? klass.collection.find_one(selector, { :fields => [ :_id ] }).nil? end |
#count(extras = false) ⇒ Integer
Get the count of matching documents in the database for the context.
72 73 74 |
# File 'lib/mongoid/contexts/mongo.rb', line 72 def count(extras = false) @count ||= klass.collection.find(selector, ).count(extras) end |
#delete_all ⇒ Integer Also known as: delete
Delete all the documents in the database matching the selector.
84 85 86 |
# File 'lib/mongoid/contexts/mongo.rb', line 84 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.
97 98 99 |
# File 'lib/mongoid/contexts/mongo.rb', line 97 def destroy_all klass.destroy_all(:conditions => selector) end |
#distinct(field) ⇒ Object
Gets an array of distinct values for the supplied field across the entire collection or the susbset given the criteria.
Example:
context.distinct(:title)
108 109 110 |
# File 'lib/mongoid/contexts/mongo.rb', line 108 def distinct(field) klass.collection.distinct(field, selector) end |
#execute ⇒ Object
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.
Example:
context.execute
Returns:
An enumerable Cursor
.
124 125 126 |
# File 'lib/mongoid/contexts/mongo.rb', line 124 def execute klass.collection.find(selector, ) || [] end |
#group ⇒ Object
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.
Example:
context.group
Returns:
A Hash
with field values as keys, arrays of documents as values.
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/mongoid/contexts/mongo.rb', line 140 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 |
#grouped(start, field, reduce) ⇒ Object
Common functionality for grouping operations. Currently used by min, max and sum. Will gsub the field name in the supplied reduce function.
285 286 287 288 289 290 291 292 |
# File 'lib/mongoid/contexts/mongo.rb', line 285 def grouped(start, field, reduce) collection = klass.collection.group( :cond => selector, :initial => { start => "start" }, :reduce => reduce.gsub("[field]", field) ) collection.empty? ? nil : collection.first[start.to_s] 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.
Example:
context.iterate { |doc| p doc }
174 175 176 177 178 179 |
# File 'lib/mongoid/contexts/mongo.rb', line 174 def iterate(&block) return caching(&block) if criteria.cached? if block_given? execute.each { |doc| yield doc } end end |
#last ⇒ Object
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.
Example:
context.last
Returns:
The last document in the collection.
192 193 194 195 196 197 198 199 |
# File 'lib/mongoid/contexts/mongo.rb', line 192 def last opts = sorting = opts[:sort] sorting = [[:_id, :asc]] unless sorting opts[:sort] = sorting.collect { |option| [ option[0], option[1].invert ] } attributes = klass.collection.find_one(selector, opts) attributes ? Mongoid::Factory.from_db(klass, attributes) : nil end |
#max(field) ⇒ Object
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.
Example:
context.max(:age)
Returns:
A numeric max value.
215 216 217 |
# File 'lib/mongoid/contexts/mongo.rb', line 215 def max(field) grouped(:max, field.to_s, Javascript.max) end |
#min(field) ⇒ Object
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.
Example:
context.min(:age)
Returns:
A numeric minimum value.
233 234 235 |
# File 'lib/mongoid/contexts/mongo.rb', line 233 def min(field) grouped(:min, field.to_s, Javascript.min) end |
#one ⇒ Object Also known as: first
Return the first result for the Context
.
Example:
context.one
Return:
The first document in the collection.
246 247 248 249 |
# File 'lib/mongoid/contexts/mongo.rb', line 246 def one attributes = klass.collection.find_one(selector, ) attributes ? Mongoid::Factory.from_db(klass, attributes) : nil end |
#process_options ⇒ Object
Filters the field list. If no fields have been supplied, then it will be empty. If fields have been defined then _type will be included as well.
296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/mongoid/contexts/mongo.rb', line 296 def fields = [:fields] if fields && fields.size > 0 && !fields.include?(:_type) if fields.kind_of?(Hash) fields[:_type] = 1 if fields.first.last != 0 # Not excluding else fields << :type end [:fields] = fields end .dup end |
#shift ⇒ Object
Return the first result for the Context
and skip it for successive calls.
Returns:
The first document in the collection.
259 260 261 262 263 |
# File 'lib/mongoid/contexts/mongo.rb', line 259 def shift document = first criteria.skip(([:skip] || 0) + 1) document end |
#sum(field) ⇒ Object
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.
Example:
context.sum(:age)
Returns:
A numeric value that is the sum.
279 280 281 |
# File 'lib/mongoid/contexts/mongo.rb', line 279 def sum(field) grouped(:sum, field.to_s, Javascript.sum) 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.
319 320 321 322 323 324 325 326 |
# File 'lib/mongoid/contexts/mongo.rb', line 319 def update_all(attributes = {}) klass.collection.update( selector, { "$set" => attributes }, :multi => true, :safe => Mongoid.persist_in_safe_mode ) end |