Class Mongoid::Contexts::Mongo
In: lib/mongoid/contexts/mongo.rb
Parent: Object

Methods

add_to_set   aggregate   avg   blank?   caching   count   delete   delete_all   destroy   destroy_all   distinct   empty?   execute   first   group   grouped   iterate   last   length   max   min   new   one   options_with_default_sorting   process_options   pull   shift   size   sum   update   update_all  

Attributes

criteria  [RW] 

Public Class methods

Create the new mongo context. This will execute the queries given the selector and options against the database.

@example Create a new context.

  Mongoid::Contexts::Mongo.new(criteria)

@param [ Criteria ] criteria The criteria to create with.

Public Instance methods

Perform an add to set on the matching documents.

@example Add to set on all matching.

  Person.where(:name => "Alex").add_to_set(:aliases, "value")

@param [ String ] field The field to add to. @param [ Object ] value The value to add.

@return [ Object ] The update value.

@since 2.1.0

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 Aggreate the context.

  context.aggregate

@return [ Hash ] A Hash with field values as keys, counts as values

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 Get the average for a field.

  context.avg(:age)

@param [ Symbol ] field The field to get the average for.

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

Determine if the context is empty or blank given the criteria. Will perform a quick has_one asking only for the id.

@example Is the context empty?

  context.blank?a

@return [ true, false ] True if blank.

Get the count of matching documents in the database for the context.

@example Get the count without skip and limit taken into consideration.

  context.count

@example Get the count with skip and limit applied.

  context.count(true)

@param [Boolean] extras True to inclued previous skip/limit

  statements in the count; false to ignore them. Defaults to `false`.

@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 collection or the susbset given the criteria.

@example Get the distinct values.

  context.distinct(:title)

@param [ Symbol ] field The field to get the values for.

@return [ Array<Object> ] The distinct values for the field.

empty?()

Alias for blank?

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 Execute the criteria on the context.

  context.execute

@return [ Cursor ] An enumerable Cursor of results.

Return the first result for the Context.

@example Get the first document.

  context.one

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

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 Get the criteria as a group.

  context.group

@return [ Hash ] Hash with 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 results.

  context.iterate { |doc| p doc }

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 Get the last document.

  context.last

@return [ Document ] The last document in the collection.

length(extras = false)

Alias for count

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 Get the max value.

  context.max(:age)

@param [ Symbol ] field The field to get the max for.

@return [ Numeric ] A numeric max value.

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 Get the min value.

  context.min(:age)

@param [ Symbol ] field The field to get the min for.

@return [ Numeric ] A numeric minimum value.

one()

Alias for first

Perform a pull on the matching documents.

@example Pull on all matching.

  Person.where(:name => "Alex").pull(:aliases, "value")

@param [ String ] field The field to pull from. @param [ Object ] value The value to pull.

@return [ Object ] The update value.

@since 2.1.0

Return the first result for the Context and skip it for successive calls.

@example Get the first document and shift.

  context.shift

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

size(extras = false)

Alias for count

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 Get the sum for a field.

  context.sum(:age)

@param [ Symbol ] field The field who‘s values to sum.

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

update(attributes = {})

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.4

Protected Instance methods

Iterate over each Document in the results and cache the collection.

@example Execute with caching.

  context.caching

Common functionality for grouping operations. Currently used by min, max and sum. Will gsub the field name in the supplied reduce function.

@example Execute the group function.

  context.group(0, :avg, "")

@param [ Object ] start The value to start the map/reduce with. @param [ String ] field The field to aggregate. @param [ String ] reduce The reduce JS function.

@return [ Numeric ] A numeric result.

Get the options hash with the default sorting options provided.

@example Get the options.

  criteria.options_with_default_sorting

@return [ Hash ] The options.

@since 2.3.2

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.

@example Process the field list.

  context.process_options

@return [ Hash ] The options.

[Validate]