It seems they are the same. But is there any really difference between the two? The only obvious thing is of the findOneAndUpdate()
which will only match to one document.
findOneAndUpdate updates the very first document based on the filter criteria. With findAndModify you can do a lot more. You can update as well as remove the documents matching the filter criteria. Please have a look at below links for more details: https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/ https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/
Besides what you mentioned, the most conspicuous difference is:
findOneAndUpdate
allows for just updates - no deletes or replaces etc.
findAndModify
can do a lot more including replace, delete and so on. It combines the functionality of three DML operations: update, delete, replace. So it can be used as such.
Disadvantage is that it is prone to errors precisely because it combines three DML operations.
Similarities:
History:
findOneAndUpdate
, findOneAndReplace
, findOneAndDelete
are the 3 more recent methods introduced in version 3.2 of the shell to circumvent the issues mentioned above with findAndModify
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With