Bulk Operations
On this page
Overview
In this guide, you can learn how to use bulk operations in the MongoDB Java Driver.
To perform a single create, replace, update, or delete operation, you can use
the corresponding method. For example, to insert one document and replace one
document, you can use the insertOne()
and replaceOne()
methods. When you
use these methods, the MongoClient
makes one call to the database for each operation.
By using a bulk write operation, you can perform multiple write operations in fewer database calls. You can perform bulk write operations at the following levels:
Collection: You can use the
MongoCollection.bulkWrite()
method to perform bulk write operations on a single collection. In this method, each kind of write operation requires at least one database call. For example,MongoCollection.bulkWrite()
puts multiple update operations in one call, but makes two separate calls to the database for an insert operation and a replace operation.Client: If your application connects to MongoDB Server version 8.0 or later, you can use the
MongoClient.bulkWrite()
method to perform bulk write operations on multiple collections and databases in the same cluster. This method performs all write operations in one database call.
Collection Bulk Write
Bulk write operations contain one or more write operations. To perform a bulk
write operation at the collection level, pass a List
of WriteModel
documents to the MongoCollection.bulkWrite()
method. A WriteModel
is a
model that represents a write operation.
The MongoCollection.bulkWrite()
method performs each kind of write
operation in a separate database call. For example, when you pass DeleteOneModel
,
DeleteManyModel
, and ReplaceOneModel
objects to the method, it performs
two calls: one for the delete operations and one for the replace operation.
Note
When the client splits operations into separate database calls, it might reorder operations for efficiency if the bulk write operation is not ordered. To learn more about operation execution order, see the Order of Execution section.
The following sections show how to create and use each WriteModel
document. The examples in each section use the following documents in the
people
collection:
{ "_id": 1, "name": "Karen Sandoval", "age": 31 } { "_id": 2, "name": "William Chin", "age": 54 } { "_id": 8, "name": "Shayla Ray", "age": 20 }
For more information about the methods and classes mentioned in this section, see the following API documentation:
Insert Operation
To perform an insert operation, create an InsertOneModel
specifying
the document you want to insert. To insert multiple documents, you must
create an InsertOneModel
for each document you want to insert.
Example
The following example creates an InsertOneModel
for two documents
describing people:
InsertOneModel<Document> juneDoc = new InsertOneModel<>(new Document("name", "June Carrie") .append("age", 17)); InsertOneModel<Document> kevinDoc = new InsertOneModel<>(new Document("name", "Kevin Moss") .append("age", 22));
Important
When performing a bulkWrite()
, the InsertOneModel
cannot
insert a document with an _id
that already exists in the
collection. Instead, the method throws a MongoBulkWriteException
.
The following example tries to insert two documents where the _id
is
1
and 3
:
try { List<WriteModel<Document>> bulkOperations = new ArrayList<>(); // Creates instructions to insert documents InsertOneModel<Document> doc1 = new InsertOneModel<>(new Document("_id", 1)); InsertOneModel<Document> doc3 = new InsertOneModel<>(new Document("_id", 3)); bulkOperations.add(doc1); bulkOperations.add(doc3); // Runs a bulk write operation for the specified insert WriteModels collection.bulkWrite(bulkOperations); // Prints a message if any exceptions occur during the bulk write operation } catch (MongoBulkWriteException e){ System.out.println("A MongoBulkWriteException occurred with the following message: " + e.getMessage()); }
The following shows the output of the preceding code:
A MongoBulkWriteException occurred with the following message: Bulk write operation error on server sample-shard-00-02.pw0q4.mongodb.net:27017. Write errors: [BulkWriteError{index=0, code=11000, message='E11000 duplicate key error collection: crudOps.bulkWrite index: _id_ dup key: { _id: 1 }', details={}}].
To see why the document with the _id
of 3
didn't insert, see
the Order of Execution section.
For more information about the methods and classes mentioned in this section, see the InsertOneModel API documentation.
Replace Operation
To perform a replace operation, create a ReplaceOneModel
specifying
a query filter for the document you want to replace with the replacement
document.
Important
When performing a bulkWrite()
, the ReplaceOneModel
cannot
make changes to a document that violate unique index constraints on
the collection, and the model does not replace a document if there
are no matches to your query filter.
Example
The following example creates a ReplaceOneModel
to
replace a document where the _id
is 1
with a document that
contains an added location
field:
ReplaceOneModel<Document> celineDoc = new ReplaceOneModel<>( Filters.eq("_id", 1), new Document("name", "Celine Stork") .append("location", "San Diego, CA"));
For more information about the methods and classes mentioned in this section, see the following resources:
ReplaceOneModel API documentation
Unique indexes Server Manual Explanation
Update Operation
To perform an update operation, create an UpdateOneModel
or an
UpdateManyModel
specifying a query filter for documents you want to
update with what the updates are.
The UpdateOneModel
updates the first document that matches your query
filter and the UpdateManyModel
updates all the documents that
match your query filter.
Important
When performing a bulkWrite()
, the UpdateOneModel
and
UpdateManyModel
cannot make changes to a document that violate
unique index constraints on the collection, and the models do not
update any documents if there are no matches to your query filter.
Example
The following example creates an UpdateOneModel
to update
the age
field in a document where the _id
is 2
:
UpdateOneModel<Document> updateDoc = new UpdateOneModel<>( Filters.eq("_id", 2), Updates.set("age", 31));
For more information about the methods and classes mentioned in this section, see the following resources:
UpdateOneModel API documentation
UpdateManyModel API documentation
unique indexes Server Manual Explanation
Delete Operation
To perform a delete operation, create a DeleteOneModel
or a
DeleteManyModel
specifying a query filter for documents you want to
delete.
The DeleteOneModel
deletes the first document that matches your query
filter and the DeleteManyModel
deletes all the documents that
match your query filter.
Important
When performing a bulkWrite()
, the DeleteOneModel
and
DeleteManyModel
do not delete any documents if there are no matches
to your query filter.
Example
The following example creates a DeleteOneModel
to delete
a document where the _id
is 1
:
DeleteOneModel<Document> deleteDoc = new DeleteOneModel<>(Filters.eq("_id", 1));
For more information about the methods and classes mentioned in this section, see the following API documentation:
Order of Execution
The bulkWrite()
method accepts an optional BulkWriteOptions
as a
second parameter to specify whether the execution of the bulk operations is
ordered or unordered.
By default, the bulkWrite()
method executes bulk operations in
order. This means that the bulk operations execute in the order you
added them to the list until an error occurs, if any.
Example
The following example performs these bulk operations:
An operation that inserts a document with a
name
value of"Zaynab Omar"
and anage
value of37
An operation that replaces the document where the
_id
is1
with a new document that contains thelocation
fieldAn operation that updates the document with a
name
value of"Zaynab Omar"
and changes thename
to"Zaynab Hassan"
An operation that deletes all documents where the
age
value is greater than50
List<WriteModel<Document>> bulkOperations = new ArrayList<>(); // Creates instructions to insert a document InsertOneModel<Document> insertDoc = new InsertOneModel<>(new Document("_id", 6) .append("name", "Zaynab Omar") .append("age", 37)); // Creates instructions to replace the first document matched by the query ReplaceOneModel<Document> replaceDoc = new ReplaceOneModel<>(Filters.eq("_id", 1), new Document("name", "Sandy Kane") .append("location", "Helena, MT")); // Creates instructions to update the first document matched by the query UpdateOneModel<Document> updateDoc = new UpdateOneModel<>(Filters.eq("name", "Zaynab Omar"), Updates.set("name", "Zaynab Hassan")); // Creates instructions to delete all documents matched by the query DeleteManyModel<Document> deleteDoc = new DeleteManyModel<>(Filters.gt("age", 50)); bulkOperations.add(insertDoc); bulkOperations.add(replaceDoc); bulkOperations.add(updateDoc); bulkOperations.add(deleteDoc); // Runs a bulk write operation for the specified the insert, replace, update, and delete WriteModels in order collection.bulkWrite(bulkOperations);
After running this example, your collection contains the following document:
{ "_id": 1, "name": "Sandy Kane", "location": "Helena, MT" } { "_id": 8, "name": "Shayla Ray", "age": 20 } { "_id": 6, "name": "Zaynab Hassan", "age": 37 }
Unordered Execution
You can also execute bulk operations in any order by specifying "false"
to the order()
method on BulkWriteOptions
. This means that
all the write operations execute regardless of errors and if any errors occur
the bulk operation reports them at the end.
Adding to the preceding example, including the following specifies the bulk operations to execute in any order:
BulkWriteOptions options = new BulkWriteOptions().ordered(false); // Runs a bulk write operation for the specified insert, replace, update, and delete WriteModels in any order collection.bulkWrite(bulkOperations, options);
Note
Unordered bulk operations do not guarantee order of execution. The order can differ from the way you list them to optimize the runtime.
In the preceding example, if the bulkWrite()
method decided to
perform the insert operation after the update operation, nothing
changes with the update operation because the document does not exist
at that point in time. Your collection then contains the following
documents:
{ "_id": 1, "name": "Sandy Kane", "location": "Helena, MT" } { "_id": 8, "name": "Shayla Ray", "age": 20 } { "_id": 6, "name": "Zaynab Omar", "age": 37 }
For more information about the methods and classes mentioned in this section, see the following API documentation:
Client Bulk Write
When connecting to a deployment running MongoDB Server 8.0 or later,
you can use the MongoClient.bulkWrite()
method to write
to multiple databases and collections in the same cluster. The MongoClient.bulkWrite()
method performs all write operations in a single call.
The MongoClient.bulkWrite()
method takes a
list of ClientNamespacedWriteModel
instances to represent different write operations.
You can construct instances of the ClientNamespacedWriteModel
interface by using
instance methods. For example, an instance of ClientNamespacedInsertOneModel
represents an
operation to insert one document, and you can create this model by using
the ClientNamespacedWriteModel.insertOne()
method.
The models and their corresponding instance methods are described in the table below.
Model | Instance Method | Description | Parameters |
---|---|---|---|
|
| Creates a model to insert a document into the |
|
|
| Creates a model to update the first document in the |
You must pass a value for either the |
|
| Creates a model to update all documents in the |
You must pass a value for either the |
|
| Creates a model to replace the first document in the |
|
|
| Creates a model to delete the first document in the |
|
|
| Creates a model to delete all documents in the |
|
The following sections provide examples of how to use the client bulkWrite()
method.
Insert Example
This example shows how to use the bulkWrite()
method to insert
two documents. One document is inserted into the db.people
collection, while
the other document is inserted into the db.things
collection.
The MongoNamespace
instance defines the databases and collections that
each write operation applies to.
MongoNamespace peopleNamespace = new MongoNamespace("db", "people"); MongoNamespace thingsNamespace = new MongoNamespace("db", "things"); List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>(); ClientNamespacedInsertOneModel insertDocument1 = new ClientNamespacedWriteModel .insertOne( peopleNamespace, new Document("name", "Julia Smith") ); ClientNamespacedInsertOneModel insertDocument2 = new ClientNamespacedWriteModel .insertOne( thingsNamespace, new Document("object", "washing machine") ); bulkOperations.add(insertDocument1); bulkOperations.add(insertDocument2); ClientBulkWriteResult result = mongoClient.bulkWrite(bulkOperations);
Replace Example
The following example shows how to use the bulkWrite()
method to replace
existing documents in the db.people
and db.things
collections.
MongoNamespace peopleNamespace = new MongoNamespace("db", "people"); MongoNamespace thingsNamespace = new MongoNamespace("db", "things"); List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>(); bulkOperations.add(ClientNamespacedWriteModel.replaceOne( peopleNamespace, Filters.eq("_id", 1), new Document("name", "Frederic Hilbert") ) ); bulkOperations.add(ClientNamespacedWriteModel.replaceOne( thingsNamespace, Filters.eq("_id", 1), new Document("object", "potato") ) ); ClientBulkWriteResult result = mongoClient.bulkWrite(bulkOperations);
After this example runs successfully, the document that has an _id
value of 1
in the people
collection is replaced with a new document. The document in
the things
collection that has an _id
value of 1
is replaced with a new document.
Bulk Write Options
You can pass an instance of ClientBulkWriteOptions
to the bulkWrite()
method to specify options when running the bulk write operations.
Order of Execution Example
By default, the individual operations in a bulk operation are executed in the
order that you specify them until an error occurs, or until they execute
successfully. However, you can pass false
to the ordered()
method on
the ClientBulkWriteOptions
interface to perform write operations in an
unordered way. When using the unordered option, an error-producing operation
does not prevent execution of other write operations in the call to the
bulkWrite()
method.
The following code sets the ordered()
method on an
instance of ClientBulkWriteOptions
and performs a bulk write operation to
insert multiple documents.
MongoNamespace namespace = new MongoNamespace("db", "people"); ClientBulkWriteOptions options = new ClientBulkWriteOptions().ordered(false); List<ClientNamespacedWriteModel> bulkOperations = new ArrayList<>(); bulkOperations.add( ClientNamespacedWriteModel.insertOne( namespace, new Document("_id", 1).append("name", "Rudra Suraj") ) ); // Causes a duplicate key error bulkOperations.add( ClientNamespacedWriteModel.insertOne( namespace, new Document("_id", 1).append("name", "Mario Bianchi") ) ); bulkOperations.add( ClientNamespacedWriteModel.insertOne( namespace, new Document("name", "Wendy Zhang") ) ); ClientBulkWriteResult result = mongoClient.bulkWrite(bulkOperations, options);
Even though the write operation inserting a document with a duplicate key results in an error, the other operations are executed because the write operation is unordered.
Summary
MongoCollection.bulkWrite()
To perform a bulk operation, you create and pass a list of
WriteModel
instances to the bulkWrite()
method.
There are 6 different WriteModel
subtypes: InsertOneModel
,
ReplaceOneModel
, UpdateOneModel
, UpdateManyModel
,
DeleteOneModel
and DeleteManyModel
.
There are two ways to execute the bulkWrite()
method:
Ordered, which performs the bulk operations in order until an error occurs, if any
Unordered, which performs all the bulk operations in any order and reports errors at the end, if any
MongoClient.bulkWrite()
When connecting to a deployment running MongoDB Server version 8.0 or later, you
can use the MongoClient.bulkWrite()
method to perform bulk operations on multiple
databases and collections at once.
To perform a client bulk operation, you create an pass a list of
ClientNamespacedWriteModel
instances to this method.
There are six subtypes of ClientNamespacedWriteModel
that are used to
represent write operations. To construct these write models, you can use the
corresponding ClientNamespacedWriteModel
methods insertOne()
, updateOne()
,
updateMany()
, replaceOne()
, deleteOne()
, and deleteMany()
. These
methods take a MongoNamespace
object that defines which
database and collection to write to.
The MongoClient.bulkWrite()
method can also take a ClientBulkWriteOptions
object to specify different options for how the command is executed.
To learn more about the client bulkWrite
command, see the
bulkWrite() method reference in the MongoDB Server
Manual.