mongodbHow to bulk write in MongoDB?
MongoDB provides a bulk write operation that allows users to perform multiple write operations in one request. This operation can be used to insert, update, and delete multiple documents in a single request.
Example code
db.collection.bulkWrite([
{ insertOne: { document: { a: 1 } } },
{ updateOne: { filter: { a: 2 }, update: { $set: { a: 2 } }, upsert: true } },
{ deleteOne: { filter: { a: 3 } } }
])
Output example
{
"acknowledged" : true,
"deletedCount" : 1,
"insertedCount" : 1,
"matchedCount" : 1,
"upsertedCount" : 1,
"insertedIds" : [
ObjectId("5f3f9f9f8f9f9f9f9f9f9f9f")
],
"upsertedIds" : [
ObjectId("5f3f9f9f8f9f9f9f9f9f9f9f")
]
}
Code explanation
db.collection.bulkWrite()
: This is the method used to perform the bulk write operation.insertOne
: This is the operation used to insert a single document.updateOne
: This is the operation used to update a single document.deleteOne
: This is the operation used to delete a single document.filter
: This is the filter used to specify which documents should be affected by the operation.update
: This is the update document used to specify the changes to be made to the documents.upsert
: This is a boolean flag used to specify whether a new document should be inserted if no documents match the filter.
Helpful links
More of Mongodb
- How to check the version of MongoDB?
- How to use watch in MongoDB?
- How to list MongoDB users?
- How to remove a field from MongoDB?
- How to create a many to many relation in MongoDB?
- How to use MongoDB queue?
- What is MongoDB default port?
- How to update an array element in MongoDB?
- How to select specific fields in MongoDB query?
- How to check if array is empty in MongoDB?
See more codes...