I think that findOneAndUpdate
carries out an atomic operation, so I'm assuming that updateOne
does not.
Why would you choose updateOne
over findOneAndUpdate
and avoid an atomic operation and have to spend extra time checking if the updates were atomic?
I would appreciate some insight or a use case.
findOneAndUpdate returns a document whereas updateOne does not (it just returns the _id if it has created a new document).
When true , findOneAndUpdate() either: Creates a new document if no documents match the filter . For more details see upsert behavior.
With the exception of an unindexed upsert, findOneAndUpdate() is atomic. That means you can assume the document doesn't change between when MongoDB finds the document and when it updates the document, unless you're doing an upsert.
The findOneAndUpdate() function is used to find a matching document and update it according to the update arg, passing any options, and returns the found document (if any) to the callback.
I think that
findOneAndUpdate
carries out an atomic operation, so I'm assuming thatupdateOne
does not.
Why are you assuming that?
findOneAndUpdate
returns a document whereas updateOne
does not (it just returns the _id
if it has created a new document).
I think that's the main difference. So the use case of updateOne
is when you don't need the document and want to save a bit of time and bandwidth.
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