Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between findAndModify and update in MongoDB?

People also ask

What is difference between update and findOneAndUpdate?

The . findOneAndUpdate method issues a mongodb . findAndModify update command and returns the found document (if any) to the callback or return the modified document rather than the original if the new option is true and the . update execute the query as an update() operation.

What is the difference between update and save in MongoDB?

In MongoDB, the 'update ()' and 'save ()' methods are used to update the document into a collection. The 'update ()' method is used to update the values present in the existing document whereas 'save ()' method replaces the entire existing document with the new document which is passed in the 'save ()' method.

How do I use findAndModify in MongoDB?

MongoDB – FindAndModify() Method. The findAndModify() method modifies and return a single document that matches the given criteria. By default, this method returns a pre-modification document. To return the document with the modifications made on the update, use the new option and set its value to true.

What is update query in MongoDB?

MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.


If you fetch an item and then update it, there may be an update by another thread between those two steps. If you update an item first and then fetch it, there may be another update in-between and you will get back a different item than what you updated.

Doing it "atomically" means you are guaranteed that you are getting back the exact same item you are updating - i.e. no other operation can happen in between.


findAndModify returns the document, update does not.

If I understood Dwight Merriman (one of the original authors of mongoDB) correctly, using update to modify a single document i.e.("multi":false} is also atomic. Currently, it should also be faster than doing the equivalent update using findAndModify.


From the MongoDB docs (emphasis added):

  • By default, both operations modify a single document. However, the update() method with its multi option can modify more than one document.

  • If multiple documents match the update criteria, for findAndModify(), you can specify a sort to provide some measure of control on which document to update. With the default behavior of the update() method, you cannot specify which single document to update when multiple documents match.

  • By default, findAndModify() method returns the pre-modified version of the document. To obtain the updated document, use the new option. The update() method returns a WriteResult object that contains the status of the operation. To return the updated document, use the find() method. However, other updates may have modified the document between your update and the document retrieval. Also, if the update modified only a single document but multiple documents matched, you will need to use additional logic to identify the updated document.

  • Before MongoDB 3.2 you cannot specify a write concern to findAndModify() to override the default write concern whereas you can specify a write concern to the update() method since MongoDB 2.6.

When modifying a single document, both findAndModify() and the update() method atomically update the document.


One useful class of use cases is counters and similar cases. For example, take a look at this code (one of the MongoDB tests): find_and_modify4.js.

Thus, with findAndModify you increment the counter and get its incremented value in one step. Compare: if you (A) perform this operation in two steps and somebody else (B) does the same operation between your steps then A and B may get the same last counter value instead of two different (just one example of possible issues).