class Mongoid::Contextual::FindAndModify
Attributes
@attribute [r] criteria The criteria for the context. @attribute [r] options The command options. @attribute [r] update The updates. @attribute [r] query The Moped query.
@attribute [r] criteria The criteria for the context. @attribute [r] options The command options. @attribute [r] update The updates. @attribute [r] query The Moped query.
@attribute [r] criteria The criteria for the context. @attribute [r] options The command options. @attribute [r] update The updates. @attribute [r] query The Moped query.
@attribute [r] criteria The criteria for the context. @attribute [r] options The command options. @attribute [r] update The updates. @attribute [r] query The Moped query.
Public Class Methods
Initialize the find and modify command, used for MongoDB's $findAndModify.
@example Initialize the command.
FindAndModify.new(criteria, { "$set" => { likes: 1 }})
@param [ Criteria ] criteria The criteria. @param [ Hash ] update The updates. @param [ Hash ] options The command options.
@option options [ true, false ] :new Return the updated document. @option options [ true, false ] :remove Delete the first document. @option options [ true, false ] :upsert Create the document if it doesn't exist.
@since 3.0.0
# File lib/mongoid/contextual/find_and_modify.rb, line 27 def initialize(collection, criteria, update, options = {}) @collection, @criteria, @options, @update = collection, criteria, options, update.mongoize @query = collection.find(criteria.selector) apply_criteria_options end
Public Instance Methods
Get the result of the $findAndModify.
@example Get the result.
find_and_modify.result
@return [ Hash ] The result of the command.
@since 3.0.0
# File lib/mongoid/contextual/find_and_modify.rb, line 42 def result query.modify(update, options) end
Private Instance Methods
Apply criteria specific options - query, sort, fields.
@api private
@example Apply the criteria options
find_and_modify.apply_criteria_options
@return [ nil ] Nothing.
@since 3.0.0
# File lib/mongoid/contextual/find_and_modify.rb, line 58 def apply_criteria_options if spec = criteria.options[:sort] query.sort(spec) end if spec = criteria.options[:fields] query.select(spec) end end