Class: Mongoid::Contextual::FindAndModify

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

Instance Attribute Summary collapse

Attributes included from Command

#collection, #collection The collection to query against.

Instance Method Summary collapse

Methods included from Command

#command, #session

Constructor Details

#initialize(collection, criteria, update, options = {}) ⇒ FindAndModify

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.

  • :upsert (true, false)

    Create the document if it doesn’t exist.

Since:

  • 3.0.0



28
29
30
31
32
33
# File 'lib/mongoid/contextual/find_and_modify.rb', line 28

def initialize(collection, criteria, update, options = {})
  @collection, @criteria, @options, @update =
    collection, criteria, options, update.mongoize
  @query = collection.find(criteria.selector)
  apply_criteria_options
end

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



11
12
13
# File 'lib/mongoid/contextual/find_and_modify.rb', line 11

def criteria
  @criteria
end

#criteria The criteria for the context.(Thecriteria) ⇒ Object (readonly)



11
# File 'lib/mongoid/contextual/find_and_modify.rb', line 11

attr_reader :criteria, :options, :update, :query

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/mongoid/contextual/find_and_modify.rb', line 11

def options
  @options
end

#options The command options.(Thecommandoptions.) ⇒ Object (readonly)



11
# File 'lib/mongoid/contextual/find_and_modify.rb', line 11

attr_reader :criteria, :options, :update, :query

#queryObject (readonly)

Returns the value of attribute query.



11
12
13
# File 'lib/mongoid/contextual/find_and_modify.rb', line 11

def query
  @query
end

#query The Moped query.(TheMopedquery.) ⇒ Object (readonly)



11
# File 'lib/mongoid/contextual/find_and_modify.rb', line 11

attr_reader :criteria, :options, :update, :query

#updateObject (readonly)

Returns the value of attribute update.



11
12
13
# File 'lib/mongoid/contextual/find_and_modify.rb', line 11

def update
  @update
end

#update The updates.(Theupdates.) ⇒ Object (readonly)



11
# File 'lib/mongoid/contextual/find_and_modify.rb', line 11

attr_reader :criteria, :options, :update, :query

Instance Method Details

#resultHash

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



43
44
45
# File 'lib/mongoid/contextual/find_and_modify.rb', line 43

def result
  query.modify(update, options)
end