Use the 5 method to update multiple documents that matches with the specified filter criteria in a collection.Syntax:
Parameters:
In the above syntax, 6 points to the current database, 7 points is an existing collection name.To demonstrate the update operation, insert the following sample documents in the 8 collection.Sample Data Copy
The following modifies matching documents using the 2 method in 8 collection.Example: updateMany() Copy
Output
In the above example, the first parameter is the filter criteria specified as a document, 1 indicates that find documents whose 2 are 3. The second parameter is used to specify fields and values to be modified on the matching document in the 4 format. Use the to specify an action to perform. Here we want to set the value of fields, so use 5 operator to specify fields and updated values in 6 format. 7 modifies the 2 fields of all matching documents to 9.In the output, 0 indicates the number of documents that matched with the criteria, and 1 indicates the number of documents updated.Now, check whether it has updated a value or not using the 2 method shown below.Check Updated Document Copy
Output
The 2 method adds the specified field if it does not exist in a matching document. For example, the following will add the 4 field.Example: updateMany() Copy
Output
Execute the following 2 method to see the updated data.Check Updated Document Copy
Output
If you specify an empty filter criteria 6, then it will update all the documents. The following will update or add 4 field in all documents.Example: updateMany() Copy 0Output 1Use the 8 update operator to increase the value of the field by the specified amount. The following increases the 2 by 0 whose salary is 9.Example: $inc Operator Copy 2Output
Update Multiple FieldsYou can also specify multiple fields to update. The following updates 2 and 3 fields.Example: Update Multiple Fields Copy 4Output
Execute the following 2 method to see the updated data.Check Updated Document Copy 6Output 7The 2 method does not update any documents if no matching documents found. For example, the following will not update any documents.Example: updateMany() Copy 8Output 9Upsert - Add if not ExistSpecify 6 as a third parameter in the 7 method. The 8 adds a new document if the matching document does not found.Example: Upsert Copy 0Output 1In the above example, MongoDB adds a new document with new 9, because it cannot find a document with the 0.Update OperatorsThe following table lists the update operators which can be used with the 4 and 2 methods.MethodDescription$currentDateSets the value of a field to current date, either as a Date or a Timestamp.$incIncrements the value of the field by the specified amount.$minOnly updates the field if the specified value is less than the existing field value.$maxOnly updates the field if the specified value is greater than the existing field value.$mulMultiplies the value of the field by the specified amount.$renameRenames a field.$setSets the value of a field in a document.$setOnInsertSets the value of a field if an update results in an insert of a document. Has no effect on update operations that modify existing documents.$unsetRemoves the specified field from a document. |