Updated March 8, 2023
Introduction to Mongodb updateMany
Mongodb updateMany method is used to update all the documents which match the selection criteria from the query, we can say that the updateOne method will update only a single document but updateMany method will update all the documents which were met our selection criteria. If we have not used any selection criteria or we pass empty { } then all the documents will be updated from the collection using updateMany method. MongoDB updateMany methods will add the new field to the document, also it will return the matched count and updated rows in the result.
Syntax
Below is the syntax of updateMany methods is as follows. We can update multiple documents by using this method.
db.name_of_collection.updateMany (
{Selection criteria of update query using updateMany method}, {$set operator} {Upsert (optional), writeConcern (Optional), collation (optional), arrayFilters (Optional), hint (optional)}
)
Parameter description of findOneAndUpdate method in MongoDB.
- Name of collection – This parameter is defined as collection name which was we have used with updateMany methods to update multiple documents.
- updateMany – This method is used in MongoDB to update multiple documents from a single query.
- Selection criteria of update query – In this section we have defined the selection criteria for updating documents. Which documents we have updated it’s defined in this selection criteria.
- $set operator – This operator is used in MongoDB while updating any documents. It will update or replace the existing value with the new value.
- Upsert – It is an optional parameter used with updateMany methods in MongoDB. To use this parameter with updateMany methods we need to define this parameter value as true in our query. The default value of this parameter is false.
- Writeconcern – We can only use this parameter when we didn’t require the default value of this parameter. Type of writeConcern parameter in updateMany method is the document.
- Collation – This is also the default parameter used with updateMany methods in MongoDB. This parameter will specify the use of collation operations in MongoDB. While using this parameter with updateMany method it will allow the language-specific rules to the user.
- ArrayFilters – Type of this parameter in updateMany method is an array. This parameter shows which array we have modifying using updateMany
- Hint – This specifies that we have using the index or not to filter the data. This is also an optional parameter using updateMany method in MongoDB.
How updateMany works in Mongodb?
- MongoDB updateMany method is basically used to update multiple documents from the collection in a single query.
- All the documents were updated which was we have defined in the selection criteria. If we have not used any selection criteria, then all the documents from that particular collection will be updated.
- The below example shows that all the documents from the collection will be updated when we have not used any selection criteria.
Code:
> db.MongoDB_Update.find ()
> db.MongoDB_Update.updateMany ({ }, {$set: {name: "PQR"}})
db.MongoDB_Update.find ()
- In the above example, we can see that we have not used any selection criteria for updating documents, only we have used a set operator to update the existing value.
- While not using any criteria we can see that all the documents from the MongoDB_Update collection will be updated from the name ABC to PQR.
- Basically, there are three parameters we have used with updateMany methods in MongoDB.
- Filter
- Update
- Options
- Filter and update parameters are mandatory while options parameters are optional using the updateMany method in MongoDB.
- UpdateMany methods in MongoDB will return the acknowledgment either true or false. The matched count will return the count of documents which was matched from our selection criteria. The modified count will return the count of updated documents which was we have updated using the updateMany method.
Example
- Below is the example of updateMany methods in MongoDB are as follows.
Update multiple documents by using selection criteria –
- In the below example, we have updated multiple documents using selection criteria. We have used selection criteria on update names from ABC to MongoDB_updateMany.
- We can see that all the documents will be updated which contains the name ABC.
Code:
> db.MongoDB_Update.find ()
> db.MongoDB_Update.updateMany ({name: "ABC"}, {$set:{name: "MongoDB_updateMany"}})
> db.MongoDB_Update.find ()
Update multiple documents without using selection criteria –
- In the below example, we have updated multiple documents without using selection criteria. We have not used selection criteria so all the documents will be updated as name ABC.
- We can see that all the documents will be updated from collection MongoDB_Update.
Code:
> db.MongoDB_Update.find ()
> db.MongoDB_Update.updateMany ({ }, {$set:{name: "ABC"}})
db.MongoDB_Update.find ()
Update embedded document by using updateMany method –
- In the below example, we have updated the embedded documents. We have updated the lap_spec set for the name of the ABC field.
Code:
> db.MongoDB_Update.find ()
> db.MongoDB_Update.updateMany ({name: "ABC"}, {$set: {"lap_spec.ram": 256, "lap_spec.screen": 18.5, "lap_spec.cpu": 1.66}})
db.MongoDB_Update.find ()
Update array elements using updateMany method –
- In the below example, we have updated the array element documents. We have updated the lap_storage array for the name of ABC.
Code:
> db.MongoDB_Update.find ()
> db.MongoDB_Update.updateMany ({name: "ABC"}, {$set: {"lap_storage.0": 8, "lap_storage.1": 16, "lap_storage.2": 32, "lap_storage.3": 64, "lap_storage.4": 128, "lap_storage.5": 256}})
db.MongoDB_Update.find ()
Upsert parameter using updateMany method –
- In the below example, we have used the upsert parameter. We have updated the name as PQR to CBZ.
- In selection criteria, we have not found any name as PQR, so the upsert parameter will insert new documents in the collection, we can see that the new documents are inserted into the collection.
Code:
> db.MongoDB_Update.find ()
> db.MongoDB_Update.updateMany ({name: "PQR"}, {$set: {name: "CBZ"}}, {upsert: true})
> db.MongoDB_Update.find ()
Conclusion
MongoDB updateMany method is used to update multiple documents from the collection. I suppose we have not defined any selection in selection criteria, then all the documents will be updated from that specified collection. We have a passing filter, update, and optional parameter with the updateMany methods in MongoDB.
Recommended Articles
This is a guide to Mongodb updateMany. Here we discuss How updateMany works in Mongodb along with the examples. You may also have a look at the following articles to learn more –