Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Update multiple rows in a single MongoDB query?
To update multiple rows in a single MongoDB query, use bulk operations with initializeUnorderedBulkOp() method. This allows you to batch multiple update operations together and execute them as one atomic operation.
Syntax
var bulkOp = db.collection.initializeUnorderedBulkOp();
bulkOp.find({condition1}).updateOne({$set: {field: "value"}});
bulkOp.find({condition2}).updateOne({$set: {field: "value"}});
bulkOp.execute();
Sample Data
db.upDateMultipleRowsDemo.insertMany([
{"CustomerName": "John", "CustomerPurchaseAmount": 500},
{"CustomerName": "Chris", "CustomerPurchaseAmount": 700},
{"CustomerName": "David", "CustomerPurchaseAmount": 50},
{"CustomerName": "Larry", "CustomerPurchaseAmount": 1900}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5cd6ceb06d78f205348bc626"),
ObjectId("5cd6ceb26d78f205348bc627"),
ObjectId("5cd6ceb36d78f205348bc628"),
ObjectId("5cd6ceb46d78f205348bc629")
]
}
View Sample Data
db.upDateMultipleRowsDemo.find();
{
"_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
}
Example: Update Multiple Documents
Update John's name to "Bob" and David's purchase amount to 56544444 in a single bulk operation ?
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": []
})
Verify Results
db.upDateMultipleRowsDemo.find();
{
"_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
}
Conclusion
Use initializeUnorderedBulkOp() to update multiple documents efficiently in a single operation. This approach reduces network round trips and improves performance when updating multiple records with different conditions.
Advertisements
