Sometimes there are situations when you need to execute several MongoDB commands in the same database, but it may not be so convenient to prescribe them separately each time. In such cases, the bulkWrite() command can come to help. In this topic, we will discuss this command, talk about its features, and look at some examples. Let us begin!
The bulkWrite() operation
The bulkWrite() operation is a function that takes an array of other write operations and performs each of them in the defined order. By default, the order is step-by-step. With bulkWrite(), you can combine multiple insert, update, replace, or delete operations into a single batch. This reduces the number of round trips between your application and the MongoDB server, reducing network latency and improving overall performance. The bulkWrite() operation has the following syntax:
db.<collection>.bulkWrite(
[ {<operation_1>}, {<operation_2>}, ... ],
{
ordered: <boolean>
}
)
The first parameter is an array of operations to execute. Each write operation in the array is an object that specifies the type of operation and the document to be affected by that operation. In general, the syntax of each operation is as follows:
{
<operation>: {<operation_parameters>}
}
The operations that allowed to use with the bulkWrite():
insertOneupdateOneupdateManydeleteOnedeleteMany
The ordered parameter is optional, which default value is true. Specifies if the commands should be executed in direct order. The operation's outcomes can change when ordered is false. For instance, depending on whether the deleteOne or deleteMany operations are conducted before or after the insertOne, updateOne or updateMany, more or fewer documents may be removed.
Inserting with the bulkWrite()
In this paragraph, we will consider the main examples of using the insert command with the bulkWrite() function.
Firstly, let's discuss the insert option. The common syntax for using insertOne is the following:
db.<collection>.bulkWrite([
{ insertOne: { "document": <document> } }
])
The <document> in here is actually the document you want to insert. For instance, if we want to add a few documents that include _id and name fields, we can perform this command:
db.some_collection.bulkWrite([
{ insertOne: { "document": {_id: 1, name: "John"} } },
{ insertOne: { "document": {_id: 2, name: "Alise"} } },
{ insertOne: { "document": {_id: 3, name: "Bob"} } }
])
From the other side, you may already guess that we can get the same result using just insertMany command. And you are totally right about this.
db.some_collection.insertMany([
{_id: 1, name: "John"},
{_id: 2, name: "Alise"},
{_id: 3, name: "Bob"}
])
Thus, when it is necessary to add several records to the collection with a single command, the insertMany() function is predominantly used.
Updating with the bulkWrite()
In this section, we will look at the use of updateOne/updateMany operations with the bulkWrite().
The common syntax of the updateOne/updateMany operations is as follows:
db.<collection>.bulkWrite([
{ updateOne / updateMany:
{
"filter": <document>,
"update": <document>
}
}
])
For example, updating multiple records using different criteria would look like this:
db.some_collection.bulkWrite([
{ updateOne: {"filter": {"_id": 1}, "update": {$set: {"salary": 10000}} } },
{ updateOne: {"filter": {"_id": 7}, "update": {$set: {"salary": 5000}} } },
{ updateOne: {"filter": {"_id": 2}, "update": {$set: {"salary": 14000}} } }
])
In case you need to use the updateMany command, for example, when you want to update several records that fit the same criterion, the syntax will be at least the same:
db.some_collection.bulkWrite([
{ updateMany: {"filter": {"dept": "IT"}, "update": {$set: {"salary": 20000}} } },
{ updateMany: {"filter": {"dept": "R&D"}, "update": {$set: {"salary": 25000}} } },
{ updateMany: {"filter": {"dept": "Design"}, "update": {$set: {"salary": 21000}} } }
])Deleting with the bulkWrite()
It's time to see how to use the delete operation in the context of a bulkWrite() function.
The syntax, in this case, is also quite obvious and repeats the syntax of the deleteOne/deleteMany operation itself:
db.<collection>.bulkWrite([
{ deleteOne: {"filter": <document>}
])
The same as with the update operation, we can delete only one document for each of the criteria using deleteOne:
db.some_collection.bulkWrite([
{ deleteOne: {"filter": {"_id": 9}} },
{ deleteOne: {"filter": {"_id": 56}} },
{ deleteOne: {"filter": {"_id": 114}} }
])
Or delete all the documents matching each of the criteria using deleteMany operation:
db.some_collection.bulkWrite([
{ deleteMany: {"filter": {"company": "Google"}} },
{ deleteMany: {"filter": {"company": "Microsoft"}} },
{ deleteMany: {"filter": {"company": "Apple"}} }
])Conclusion
In this topic, you learned a function in MongoDB like bulkWrite(), which allows you to perform multiple operations using a single command. You have also learned the syntax of basic commands in the context of this function and can now freely use them all together. Let's dive into a practice part to consolidate the knowledge.