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 update one document in MongoDB?
- How to update many documents in MongoDB?
- How to use MongoDB queue?
- How to create a many to many relation in MongoDB?
- How to insert new document into MongoDB?
- How to use hint in MongoDB?
- How to work with time series data in MongoDB?
- How to use unwind in MongoDB?
See more codes...