
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to update many documents with one query in MongoDB?
To update many documents with a single query, use bulkWrite() in MongoDB. Let us create a collection with documents −
> db.demo760.insertOne({id:1,details:{Value1:100,Value2:50}}); { "acknowledged" : true, "insertedId" : ObjectId("5eb0309f5637cd592b2a4aee") } > db.demo760.insertOne({id:2,details:{Value1:60,Value2:70}}); { "acknowledged" : true, "insertedId" : ObjectId("5eb030a15637cd592b2a4aef") } > db.demo760.insertOne({id:3,details:{Value1:80,Value2:90}}); { "acknowledged" : true, "insertedId" : ObjectId("5eb030a15637cd592b2a4af0") }
Display all documents from a collection with the help of find() method −
> db.demo760.find();
This will produce the following output −
{ "_id" : ObjectId("5eb0309f5637cd592b2a4aee"), "id" : 1, "details" : { "Value1" : 100, "Value2" : 50 } } { "_id" : ObjectId("5eb030a15637cd592b2a4aef"), "id" : 2, "details" : { "Value1" : 60, "Value2" : 70 } } { "_id" : ObjectId("5eb030a15637cd592b2a4af0"), "id" : 3, "details" : { "Value1" : 80, "Value2" : 90 } }
Following is the query to update many documents with one query in MongoDB −
> db.demo760.bulkWrite([ ... { ... updateOne: { ... filter: {id: 1}, ... update: {$set: {'details.Value1': 900, 'details.Value2': 500}}, ... } ... }, ... { ... updateOne: { ... filter: {id: 2}, ... update: {$set: {'details.Value1': 1000, 'details.Value2': 2000}}, ... }, ... } ... ]) { "acknowledged" : true, "deletedCount" : 0, "insertedCount" : 0, "matchedCount" : 2, "upsertedCount" : 0, "insertedIds" : { }, "upsertedIds" : { } }
Display all documents from a collection with the help of find() method −
> db.demo760.find();
This will produce the following output −
{ "_id" : ObjectId("5eb0309f5637cd592b2a4aee"), "id" : 1, "details" : { "Value1" : 900, "Value2" : 500 } } { "_id" : ObjectId("5eb030a15637cd592b2a4aef"), "id" : 2, "details" : { "Value1" : 1000, "Value2" : 2000 } } { "_id" : ObjectId("5eb030a15637cd592b2a4af0"), "id" : 3, "details" : { "Value1" : 80, "Value2" : 90 } }
- Related Articles
- MongoDB query to update all documents matching specific IDs
- Appending an entry in one to many embedded documents with MongoDB
- MongoDB query to update each field of documents in collection with a formula?
- How to update all documents in MongoDB?
- How to update multiple documents in MongoDB?
- MongoDB query to skip documents
- Upsert many documents in MongoDB
- How to validate documents before insert or update in MongoDB?
- Update values in multiple documents with multi parameter in MongoDB?
- MongoDB query to update array with another field?
- MongoDB - Query embedded documents?
- MongoDB query to update tag
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB query to add multiple documents
- MongoDB query to group duplicate documents

Advertisements