In MongoDB
, I'm looking for a way to ATOMICally update multiple documents and return all of the updated documents in a single call.
We can do all of the following in MongoDB
:
findAndModify
or findOneAndUpdate
update(...{multi: true}
or updateMany
find
I haven't fond a way to update multiple documents and return them all in one call. Is there a way? I'm using Mongoose
as the querying package.
Atomically update multiple documents:
update(...{multi: true}
orupdateMany
Unfourtantely that is false:
However, you can simulate a transaction to atomically update multiple documents by "using a two-phase commit approach" which is described in detail there.
You may also look at the $isolated
operator, which "prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written" but it "does not provide “all-or-nothing” atomicity for write operations"
As a summary, it is not possible at mongodb level (nor the driver), but you can simulate it at your application level therefore return what you need.
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