
- 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
Update multiple rows in a single MongoDB query?
Use the concept of initializeUnorderedBulkOp(). Let us first create a collection with documents −
>db.upDateMultipleRowsDemo.insertOne({"CustomerName":"John","CustomerPurchaseAmount":500}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb06d78f205348bc626") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Chris","CustomerPurchaseAmount":700}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb26d78f205348bc627") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"David","CustomerPurchaseAmount":50}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb36d78f205348bc628") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Larry","CustomerPurchaseAmount":1900}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb46d78f205348bc629") }
Following is the query to display all documents from a collection with the help of find() method −
> db.upDateMultipleRowsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd6ceb06d78f205348bc626"), "CustomerName" : "John", "CustomerPurchaseAmount" : 500 } { "_id" : ObjectId("5cd6ceb26d78f205348bc627"), "CustomerName" : "Chris", "CustomerPurchaseAmount" : 700 } { "_id" : ObjectId("5cd6ceb36d78f205348bc628"), "CustomerName" : "David", "CustomerPurchaseAmount" : 50 } { "_id" : ObjectId("5cd6ceb46d78f205348bc629"), "CustomerName" : "Larry", "CustomerPurchaseAmount" : 1900 }
Following is the query to update multiple rows in a single query −
> var manyUpdateValue = db.upDateMultipleRowsDemo.initializeUnorderedBulkOp(); > manyUpdateValue.find({ _id: ObjectId("5cd6ceb06d78f205348bc626")}).updateOne({$set:{"CustomerName":"Bob" }}); > manyUpdateValue.find({ _id: ObjectId("5cd6ceb36d78f205348bc628")}).updateOne({$set:{"CustomerPurchaseAmount":56544444}}); > manyUpdateValue.execute(); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 0, "nUpserted" : 0, "nMatched" : 2, "nModified" : 2, "nRemoved" : 0, "upserted" : [ ] })
Let us check all the documents once again −
> db.upDateMultipleRowsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd6ceb06d78f205348bc626"), "CustomerName" : "Bob", "CustomerPurchaseAmount" : 500 } { "_id" : ObjectId("5cd6ceb26d78f205348bc627"), "CustomerName" : "Chris", "CustomerPurchaseAmount" : 700 } { "_id" : ObjectId("5cd6ceb36d78f205348bc628"), "CustomerName" : "David", "CustomerPurchaseAmount" : 56544444 } { "_id" : ObjectId("5cd6ceb46d78f205348bc629"), "CustomerName" : "Larry", "CustomerPurchaseAmount" : 1900 }
- Related Articles
- MySQL update multiple records in a single query?
- Update multiple rows in a single column in MySQL?
- Insert multiple rows in a single MySQL query
- How to get multiple rows in a single MySQL query?
- How to obtain multiple rows in a single MySQL query?
- How to update multiple rows using single WHERE clause in MySQL?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- How to insert multiple rows with single MySQL query?
- Update only a single document in MongoDB
- MongoDB query to update only a single item from voting (up and down) records?
- MongoDB findOneAndUpdate() to update a single document
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Update multiple columns of a single row MySQL?
- Single query vs multiple queries to fetch large number of rows in SAP HANA
- MongoDB query for a single field

Advertisements