- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to update multiple documents in MongoDB?
You need to use multi:true to update multiple documents. Let us first create a collection with documents −
> db.multiUpdateDemo.insertOne({"ClientName":"John","ClientAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5cda5bc0b50a6c6dd317adc8") } > db.multiUpdateDemo.insertOne({"ClientName":"Carol","ClientAge":31}); { "acknowledged" : true, "insertedId" : ObjectId("5cda5bc1b50a6c6dd317adc9") } > db.multiUpdateDemo.insertOne({"ClientName":"John","ClientAge":39}); { "acknowledged" : true, "insertedId" : ObjectId("5cda5bc3b50a6c6dd317adca") } > db.multiUpdateDemo.insertOne({"ClientName":"John","ClientAge":41}); { "acknowledged" : true, "insertedId" : ObjectId("5cda5bc5b50a6c6dd317adcb") } > db.multiUpdateDemo.insertOne({"ClientName":"David","ClientAge":35}); { "acknowledged" : true, "insertedId" : ObjectId("5cda5bc6b50a6c6dd317adcc") }
Following is the query to display all documents from a collection with the help of find() method −
> db.multiUpdateDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cda5bc0b50a6c6dd317adc8"), "ClientName" : "John", "ClientAge" : 29 } { "_id" : ObjectId("5cda5bc1b50a6c6dd317adc9"), "ClientName" : "Carol", "ClientAge" : 31 } { "_id" : ObjectId("5cda5bc3b50a6c6dd317adca"), "ClientName" : "John", "ClientAge" : 39 } { "_id" : ObjectId("5cda5bc5b50a6c6dd317adcb"), "ClientName" : "John", "ClientAge" : 41 } { "_id" : ObjectId("5cda5bc6b50a6c6dd317adcc"), "ClientName" : "David", "ClientAge" : 35 }
Following is the query to perform multi-update. The ClientName “John” for 3 clients will now have updated age using the below query −
> db.multiUpdateDemo.update({'ClientName': 'John'}, {$set: {'ClientAge': 34}}, {multi: true}); WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
Let us check the documents once again −
> db.multiUpdateDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cda5bc0b50a6c6dd317adc8"), "ClientName" : "John", "ClientAge" : 34 } { "_id" : ObjectId("5cda5bc1b50a6c6dd317adc9"), "ClientName" : "Carol", "ClientAge" : 31 } { "_id" : ObjectId("5cda5bc3b50a6c6dd317adca"), "ClientName" : "John", "ClientAge" : 34 } { "_id" : ObjectId("5cda5bc5b50a6c6dd317adcb"), "ClientName" : "John", "ClientAge" : 34 } { "_id" : ObjectId("5cda5bc6b50a6c6dd317adcc"), "ClientName" : "David", "ClientAge" : 35 }
- Related Articles
- How to Update multiple documents in a MongoDB collection using Java?
- Update values in multiple documents with multi parameter in MongoDB?
- How to update all documents in MongoDB?
- How to merge multiple documents in MongoDB?
- How to validate documents before insert or update in MongoDB?
- How to update many documents with one query in MongoDB?
- How to delete multiple documents in MongoDB using deleteMany()?
- MongoDB query to add multiple documents
- How to update array with multiple conditions in MongoDB
- MongoDB - update partial number of documents?
- Search for multiple documents in MongoDB?
- Fetch specific multiple documents in MongoDB
- MongoDB filter multiple sub-documents?
- MongoDB query to update all documents matching specific IDs
- How to update or modify the existing documents of a collection in MongoDB?

Advertisements