I am trying to update multiple documents on mongo db. which is better update method with multi = true or updateMany method? and what is the difference between them?
The updateMany() method updates all the documents in MongoDB collections that match the given query. When you update your document, the value of the _id field remains unchanged. This method can also add new fields in the document.
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.
To update multiple documents in a collection, set the multi option to true. multi is optional. If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.
To expand on Blakes Seven's answer, all the updateOne
method does is simply set the multi
option to false
as seen here (github).
And in turn all the updateMany
method does is set the multi
opion to true
as seen here (github).
So there is no "better" method to call, just pick whichever you feel suits the situation.
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