MongoDB bulkWrite()

In MongoDB the db.collection.bulkWrite() method performs multiple write operations with controls for order of execution.

Bulk write operations affect a single collection. The collection part is the name of the collection with which to perform the operations against.

Bulk Write Operations

The db.collection.bulkWrite() method can be used to perform the following write operations:

  • insertOne
  • updateOne
  • updateMany
  • replaceOne
  • deleteOne
  • deleteMany

Any of these methods can be included within a call to db.collection.bulkWrite(), and you can include different methods in the same call.

Example

Here’s an example of using db.collection.bulkWrite() to perform a bulk write operation against a collection called pets:

Suppose we insert the following documents into a collection called pets:

db.pets.insertMany([
    { _id: 1, name: "Wag", type: "Dog", weight: 20 },
    { _id: 2, name: "Bark", type: "Dog", weight: 10 },
    { _id: 3, name: "Meow", type: "Cat" },
    { _id: 4, name: "Scratch", type: "Cat" },
    { _id: 5, name: "Bruce", type: "Bat" }
    ])

We can now use db.collection.bulkWrite() to perform a bulk write operation against that collection.

Example:

db.pets.bulkWrite([
    { insertOne: { "document": { "_id": 6, "name": "Bubbles", "type": "Fish" }}},
    { updateOne : {
        "filter" : { "_id" : 2 },
        "update" : { $set : { "weight" : 15 } }
    } },
    { deleteOne : { "filter" : { "_id" : 5 } } },
    { replaceOne : {
        "filter" : { "_id" : 4 },
        "replacement" : { "name" : "Bite", "type" : "Dog", "weight": 5 }
    } }
])

Result:

{
	"acknowledged" : true,
	"deletedCount" : 1,
	"insertedCount" : 1,
	"matchedCount" : 2,
	"upsertedCount" : 0,
	"insertedIds" : {
		"0" : 6
	},
	"upsertedIds" : {
		
	}
}

In this case, we inserted one document, updated another document, deleted another, and replaced another document.

The db.collection.bulkWrite() method returns the following:

  • A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled.
  • A count for each write operation.
  • An array containing an _id for each successfully inserted or upserted documents.

View the Result

Now let’s take a look at the documents in the collection again.

db.pets.find()

Result:

{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 15 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }
{ "_id" : 4, "name" : "Bite", "type" : "Dog", "weight" : 5 }
{ "_id" : 6, "name" : "Bubbles", "type" : "Fish" }

We can see that all the changes were made as specified.

The ordered Parameter

Bulk write operations can be either ordered or unordered. By default, they’re ordered.

You can specify the order by using the ordered boolean parameter. Providing a value of true makes it an ordered list of operations, setting it to false makes it an unordered list of operations.

With an ordered list of operations, MongoDB executes the operations serially. If an error occurs during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list.

With an unordered list of operations, MongoDB can execute the operations in parallel (although this is not guaranteed). If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.

More Information

The db.collection.bulkWrite() method also accepts a writeConcern argument, which describes the level of acknowledgment requested from MongoDB for write operations.

See the MongoDB documentation for db.collection.bulkWrite() for more information.