Class Mongoid::Contexts::Enumerable
In: lib/mongoid/contexts/enumerable/sort.rb
lib/mongoid/contexts/enumerable.rb
Parent: Object

Methods

aggregate   avg   count   delete   delete_all   destroy   destroy_all   determine   distinct   execute   filter   group   iterate   length   limit   max   min   new   root   root_class   set_collection   shift   size   sort   sum   update   update_all  

Included Modules

Relations::Embedded::Atomic

Classes and Modules

Class Mongoid::Contexts::Enumerable::Sort

External Aliases

first -> one
  Get one document.

@example Get one document.

  context.one

@return [ Document ] The first document in the array.

Attributes

collection  [RW] 
criteria  [RW] 

Public Class methods

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.

Public Instance methods

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

Get the average value for the supplied field.

@example Get the average.

  context.avg(:age)

@return [ Numeric ] A numeric value that is the average.

Gets the number of documents in the array. Delegates to size.

@example Get the count.

  context.count

@return [ Integer ] The count of documents.

delete()

Alias for delete_all

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

destroy()

Alias for destroy_all

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

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.

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.

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.

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 }
length()

Alias for count

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.

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.

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.

size()

Alias for count

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.

update(attributes = nil)

Alias for update_all

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

Protected Instance methods

If the field exists, perform the comparison and set if true.

@example Compare.

  context.determine

@return [ Array<Document> ] The matching documents.

Filters the documents against the criteria‘s selector

@example Filter the documents.

  context.filter

@return [ Array ] The documents filtered.

Limits the result set if skip and limit options.

@example Limit the results.

  context.limit(documents)

@return [ Array<Document> ] The limited documents.

Set the collection to the collection of the root document.

@example Set the collection.

  context.set_collection

@return [ Collection ] The root collection.

Sorts the result set if sort options have been set.

@example Sort the documents.

  context.sort(documents)

@return [ Array<Document> ] The sorted documents.

[Validate]