Class: Mongoid::Contextual::FindAndModify

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/mongoid/contextual/find_and_modify.rb

Instance Method Summary (collapse)

Methods included from Command

#command, #criteria, #session

Constructor Details

- (FindAndModify) initialize(criteria, update, options = {})

Initialize the find and modify command, used for MongoDB's $findAndModify.

Examples:

Initialize the command.

FindAndModify.new(criteria, { "$set" => { likes: 1 }})

Parameters:

  • criteria (Criteria)

    The criteria.

  • update (Hash)

    The updates.

  • options (Hash) (defaults to: {})

    The command options.

Options Hash (options):

  • :new (true, false)

    Return the updated document.

  • :remove (true, false)

    Delete the first document.

Since:

  • 3.0.0



21
22
23
24
25
26
27
# File 'lib/mongoid/contextual/find_and_modify.rb', line 21

def initialize(criteria, update, options = {})
  @criteria = criteria
  command[:findandmodify] = criteria.klass.collection_name.to_s
  command[:update] = update unless options[:remove]
  command.merge!(options)
  apply_criteria_options
end

Instance Method Details

- (Hash) result

Get the result of the $findAndModify.

Examples:

Get the result.

find_and_modify.result

Returns:

  • (Hash)

    The result of the command.

Since:

  • 3.0.0



37
38
39
40
41
# File 'lib/mongoid/contextual/find_and_modify.rb', line 37

def result
  session.with(consistency: :strong) do |session|
    session.command(command)["value"]
  end
end