Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between update and updateMany method in mongo DB?

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?

like image 808
Tekram Patel Avatar asked Apr 19 '16 13:04

Tekram Patel


People also ask

What is updateMany in MongoDB?

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.

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.

What is multi true in MongoDB?

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.


1 Answers

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.

like image 105
Skami Avatar answered Oct 13 '22 09:10

Skami