Class: Mongoid::Persistence::Operations::Update
- Inherits:
-
Object
- Object
- Mongoid::Persistence::Operations::Update
- Defined in:
- lib/mongoid/persistence/operations/update.rb
Overview
Update is a persistence command responsible for taking a document that has already been saved to the database and saving it, depending on whether or not the document has been modified.
Before persisting the command will check via dirty attributes if the document has changed, if not, it will simply return true. If it has it will go through the validation steps, run callbacks, and set the changed fields atomically on the document. The underlying query resembles the following MongoDB query:
collection.update(
{ "_id" : 1,
{ "$set" : { "field" : "value" },
false,
false
);
For embedded documents it will use the positional locator:
collection.update(
{ "_id" : 1, "addresses._id" : 2 },
{ "$set" : { "addresses.$.field" : "value" },
false,
false
);
Instance Attribute Summary
Attributes included from Mongoid::Persistence::Operations
#conflicts, #document, #options
Instance Method Summary collapse
-
#persist ⇒ true, false
Persist the document that is to be updated to the database.
Methods included from Atomic::Positionable
Methods included from Mongoid::Persistence::Operations
#collection, #deletes, #initialize, insert, #inserts, #notifying_parent?, #parent, remove, #selector, update, #updates, upsert, #validating?
Methods included from Modification
Instance Method Details
#persist ⇒ true, false
Persist the document that is to be updated to the database. This will only write changed fields via MongoDB’s $set modifier operation.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mongoid/persistence/operations/update.rb', line 44 def persist prepare do unless updates.empty? collection.find(selector).update(positionally(selector, updates)) conflicts.each_pair do |key, value| collection.find(selector).update( positionally(selector, { key => value }) ) end end end end |