Object
Return aggregation counts of the grouped documents. This will count by the first field provided in the fields array.
@example Aggregate on a field.
person.addresses.only(:street).aggregate
@return [ Hash ] Field values as keys, count as values
# File lib/mongoid/contexts/enumerable.rb, line 20 def aggregate {}.tap do |counts| group.each_pair { |key, value| counts[key] = value.size } end end
Get the average value for the supplied field.
@example Get the average.
context.avg(:age)
@return [ Numeric ] A numeric value that is the average.
# File lib/mongoid/contexts/enumerable.rb, line 32 def avg(field) total = sum(field) total ? (total.to_f / count) : nil end
Gets the number of documents in the array. Delegates to size.
@example Get the count.
context.count
@return [ Integer ] The count of documents.
# File lib/mongoid/contexts/enumerable.rb, line 43 def count @count ||= execute.size end
Delete all the documents in the database matching the selector.
@example Delete the documents.
context.delete_all
@return [ Integer ] The number of documents deleted.
@since 2.0.0.rc.1
# File lib/mongoid/contexts/enumerable.rb, line 57 def delete_all atomically(:$pull) do set_collection count.tap do filter.each { |doc| doc.delete } end end end
Destroy all the documents in the database matching the selector.
@example Destroy the documents.
context.destroy_all
@return [ Integer ] The number of documents destroyed.
@since 2.0.0.rc.1
# File lib/mongoid/contexts/enumerable.rb, line 75 def destroy_all atomically(:$pull) do set_collection count.tap do filter.each { |doc| doc.destroy } end end end
Gets an array of distinct values for the supplied field across the entire array or the susbset given the criteria.
@example Get the list of distinct values.
context.distinct(:title)
@return [ Array<String> ] The distinct values.
# File lib/mongoid/contexts/enumerable.rb, line 92 def distinct(field) execute.collect { |doc| doc.send(field) }.uniq end
Enumerable implementation of execute. Returns matching documents for the selector, and adds options if supplied.
@example Execute the context.
context.execute
@return [ Array<Document> ] Documents that matched the selector.
# File lib/mongoid/contexts/enumerable.rb, line 103 def execute limit(sort(filter)) || [] end
Groups the documents by the first field supplied in the field options.
@example Group the context.
context.group
@return [ Hash ] Field values as keys, arrays of documents as values.
# File lib/mongoid/contexts/enumerable.rb, line 113 def group field = field_list.first execute.group_by { |doc| doc.send(field) } end
Iterate over each Document in the results. This can take an optional block to pass to each argument in the results.
@example Iterate over the documents.
context.iterate { |doc| p doc }
# File lib/mongoid/contexts/enumerable.rb, line 135 def iterate(&block) execute.each(&block) end
Get the largest value for the field in all the documents.
@example Get the max value.
context.max(:age)
@return [ Numeric ] The numerical largest value.
# File lib/mongoid/contexts/enumerable.rb, line 145 def max(field) determine(field, :>=) end
Get the smallest value for the field in all the documents.
@example Get the minimum value.
context.min(:age)
@return [ Numeric ] The numerical smallest value.
# File lib/mongoid/contexts/enumerable.rb, line 155 def min(field) determine(field, :<=) end
Get one document and tell the criteria to skip this record on successive calls.
@example Shift the documents.
context.shift
@return [ Document ] The first document in the array.
# File lib/mongoid/contexts/enumerable.rb, line 174 def shift first.tap do |document| self.criteria = criteria.skip((options[:skip] || 0) + 1) end end
Get the sum of the field values for all the documents.
@example Get the sum of the field.
context.sum(:cost)
@return [ Numeric ] The numerical sum of all the document field values.
# File lib/mongoid/contexts/enumerable.rb, line 186 def sum(field) sum = execute.inject(nil) do |memo, doc| value = doc.send(field) || 0 memo ? memo += value : value end end
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.
@example Update all matching documents.
context.update_all(:title => "Sir")
@param [ Hash ] attributes The sets to perform.
@since 2.0.0.rc.6
# File lib/mongoid/contexts/enumerable.rb, line 203 def update_all(attributes = nil) iterate do |doc| doc.update_attributes(attributes || {}) end end
If the field exists, perform the comparison and set if true.
@example Compare.
context.determine
@return [ Array<Document> ] The matching documents.
# File lib/mongoid/contexts/enumerable.rb, line 228 def determine(field, operator) matching = documents.inject(nil) do |memo, doc| value = doc.send(field) || 0 (memo && memo.send(operator, value)) ? memo : value end end
Filters the documents against the criteria’s selector
@example Filter the documents.
context.filter
@return [ Array ] The documents filtered.
# File lib/mongoid/contexts/enumerable.rb, line 218 def filter documents.select { |document| document.matches?(selector) } end
Limits the result set if skip and limit options.
@example Limit the results.
context.limit(documents)
@return [ Array<Document> ] The limited documents.
# File lib/mongoid/contexts/enumerable.rb, line 241 def limit(documents) skip, limit = options[:skip], options[:limit] if skip && limit return documents.slice(skip, limit) elsif limit return documents.first(limit) elsif skip return documents.slice(skip..-1) end documents end
# File lib/mongoid/contexts/enumerable.rb, line 253 def root @root ||= documents.first.try(:_root) end
# File lib/mongoid/contexts/enumerable.rb, line 257 def root_class @root_class ||= root ? root.class : nil end
Set the collection to the collection of the root document.
@example Set the collection.
context.set_collection
@return [ Collection ] The root collection.
# File lib/mongoid/contexts/enumerable.rb, line 267 def set_collection @collection = root.collection if root && !root.embedded? end
Sorts the result set if sort options have been set.
@example Sort the documents.
context.sort(documents)
@return [ Array<Document> ] The sorted documents.
# File lib/mongoid/contexts/enumerable.rb, line 277 def sort(documents) return documents if options[:sort].blank? documents.sort_by do |document| options[:sort].map do |key, direction| Sort.new(document.read_attribute(key), direction) end end end
Create the new enumerable context. This will need the selector and options from a Criteria and a documents array that is the underlying array of embedded documents from a has many association.
@example Create a new context.
Mongoid::Contexts::Enumerable.new(criteria)
@param [ Criteria ] criteria The criteria for the context.
# File lib/mongoid/contexts/enumerable.rb, line 126 def initialize(criteria) @criteria = criteria end
Generated with the Darkfish Rdoc Generator 2.