Docs Menu
Docs Home
/ / /
Java Reactive Streams Driver
/

Bulk Write Operations

On this page

  • Overview
  • Sample Data
  • Define the Write Operations
  • Insert Operations
  • Update Operations
  • Replace Operations
  • Delete Operations
  • Call the bulkWrite() Method
  • Customize Bulk Write Operations
  • Additional Information
  • API Documentation

In this guide, you can learn how to perform multiple write operations in a single database call by using bulk write operations.

Consider a scenario in which you want to insert a document into a collection, update multiple other documents, then delete a document. If you use individual methods, each operation requires its own database call. This guide shows you how to use bulk write operations to reduce the number of calls to the database.

The examples in this guide use the sample_restaurants.restaurants collection from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started tutorial.

Important

Project Reactor Library

This guide uses the Project Reactor library to consume Publisher instances returned by the Java Reactive Streams driver methods. To learn more about the Project Reactor library and how to use it, see Getting Started in the Reactor documentation. To learn more about how we use Project Reactor library methods in this guide, see the Write Data to MongoDB guide.

For each write operation you want to perform, create an instance of one of the following classes:

  • InsertOneModel

  • UpdateOneModel

  • UpdateManyModel

  • ReplaceOneModel

  • DeleteOneModel

  • DeleteManyModel

Then, pass a list of these instances to the bulkWrite() method.

The following sections show how to create and use instances of the preceding classes.

To perform an insert operation, create an instance of InsertOneModel and pass in the document you want to insert.

The following example creates an instance of InsertOneModel:

InsertOneModel<Document> operation = new InsertOneModel<>(
new Document("name", "Mongo's Deli")
.append("cuisine", "Sandwiches"));

To insert multiple documents, create an instance of InsertOneModel for each document.

To update a document, create an instance of UpdateOneModel and pass in the following arguments:

  • Query filter that specifies the criteria used to match documents in your collection.

  • Update operation you want to perform. For more information about update operations, see the Field Update Operators guide in the MongoDB Server manual.

UpdateOneModel updates the first document that matches your query filter.

The following example creates an instance of UpdateOneModel:

UpdateOneModel<Document> operation = new UpdateOneModel<>(
eq("name", "Mongo's Deli"),
set("cuisine", "Sandwiches and Salads"));

To update multiple documents, create an instance of UpdateManyModel and pass in the same arguments. UpdateManyModel updates all documents that match your query filter.

The following example creates an instance of UpdateManyModel:

UpdateManyModel<Document> operation = new UpdateManyModel<>(
eq("name", "Mongo's Deli"),
set("cuisine", "Sandwiches and Salads"));

A replace operation removes all fields and values of a specified document, aside from the _id field, and replaces them with new ones. To perform a replace operation, create an instance of ReplaceOneModel and pass in a query filter and the fields and values you want to store in the matching document.

The following example creates an instance of ReplaceOneModel:

ReplaceOneModel<Document> operation = new ReplaceOneModel<>(
eq("name", "Original Pizza"),
new Document("name", "Mongo's Pizza")
.append("borough", "Manhattan"));

To replace multiple documents, create an instance of ReplaceOneModel for each document.

To delete a document, create an instance of DeleteOneModel and pass in a query filter specifying the document you want to delete. DeleteOneModel removes only the first document that matches your query filter.

The following example creates an instance of DeleteOneModel:

DeleteOneModel<Document> operation = new DeleteOneModel<>(
eq("restaurant_id", "5678"));

To delete multiple documents, create an instance of DeleteManyModel and pass in a query filter specifying the documents you want to delete. DeleteManyModel removes all documents that match your query filter.

The following example creates an instance of DeleteManyModel:

DeleteManyModel<Document> operation = new DeleteManyModel<>(
eq("name", "Mongo's Deli"));

After you define a class instance for each operation you want to perform, pass a list of these instances to the bulkWrite() method. By default, the method runs the operations in the order they're defined in the list.

The following example performs multiple write operations by using the bulkWrite() method:

Publisher<BulkWriteResult> bulkWritePublisher = restaurants.bulkWrite(
Arrays.asList(new InsertOneModel<>(
new Document("name", "Mongo's Deli")
.append("cuisine", "Sandwiches")
.append("borough", "Manhattan")
.append("restaurant_id", "1234")),
new InsertOneModel<>(new Document("name", "Mongo's Deli")
.append("cuisine", "Sandwiches")
.append("borough", "Brooklyn")
.append("restaurant_id", "5678")),
new UpdateManyModel<>(eq("name", "Mongo's Deli"),
set("cuisine", "Sandwiches and Salads")),
new DeleteOneModel<>(eq("restaurant_id", "1234"))));
BulkWriteResult bulkResult = Mono.from(bulkWritePublisher).block();
System.out.println(bulkResult.toString());
AcknowledgedBulkWriteResult{insertedCount=2, matchedCount=2, removedCount=1, modifiedCount=2, upserts=[], inserts=[BulkWriteInsert{index=0, id=BsonObjectId{value=66a7e0a6c08025218b657208}}, BulkWriteInsert{index=1, id=BsonObjectId{value=66a7e0a6c08025218b657209}}]}

If any of the write operations fail, the Java Reactive Streams driver signals a MongoBulkWriteException and does not perform any further individual operations. MongoBulkWriteException includes a BulkWriteError that can be accessed by using the MongoBulkWriteException.getWriteErrors() method, which provides details of the individual failure.

Note

When the Java Reactive Streams driver runs a bulk operation, it uses the writeConcern of the collection on which the operation is running. The driver reports all write concern errors after attempting all operations, regardless of execution order.

The BulkWriteOptions class contains methods that modify the behavior of the bulkWrite() method. To use the BulkWriteOptions class, construct a new instance of the class, then call one or more of its methods to modify the write operation. You can chain these method calls together. To modify the behavior of the write operation, pass the class instance as the last argument to the bulkWrite() method.

You can use the following methods in the BulkWriteOptions class to modify a write method. All methods are optional.

Method
Description
bypassDocumentValidation(Boolean bypassDocumentValidation)
Specifies whether the bulk write operation bypasses document validation. This lets you perform write operations on documents that don't meet the schema validation requirements, if any exist. For more information about schema validation, see Schema Validation in the MongoDB Server manual.
comment(Bson comment)
Attaches a Bson comment to the operation. For more information, see the insert command fields guide in the MongoDB Server manual.
comment(String comment)
Attaches a String comment to the operation. For more information, see the insert command fields guide in the MongoDB Server manual.
let(Bson variables)
Specifies a map of parameter names and values. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual.
ordered(Boolean ordered)
If set to True, the driver performs the individual operations in the order provided. If an individual operation fails, the driver will not execute any subsequent individual operations.
Defaults to True.

The following example calls the bulkWrite() method from the preceding example but sets the ordered option to False:

Publisher<BulkWriteResult> bulkWritePublisher = restaurants.bulkWrite(
Arrays.asList(new InsertOneModel<>(
new Document("name", "Mongo's Deli")
.append("cuisine", "Sandwiches")
.append("borough", "Manhattan")
.append("restaurant_id", "1234")),
new InsertOneModel<>(new Document("name", "Mongo's Deli")
.append("cuisine", "Sandwiches")
.append("borough", "Brooklyn")
.append("restaurant_id", "5678")),
new UpdateManyModel<>(eq("name", "Mongo's Deli"),
set("cuisine", "Sandwiches and Salads")),
new DeleteOneModel<>(eq("restaurant_id", "1234"))),
new BulkWriteOptions().ordered(false));
BulkWriteResult bulkResult = Mono.from(bulkWritePublisher).block();
System.out.println(bulkResult.toString());
AcknowledgedBulkWriteResult{insertedCount=2, matchedCount=2, removedCount=1, modifiedCount=2, upserts=[], inserts=[BulkWriteInsert{index=0, id=BsonObjectId{value=66a7e03cce430c5854b6caf9}}, BulkWriteInsert{index=1, id=BsonObjectId{value=66a7e03cce430c5854b6cafa}}]}

If any of the write operations in an unordered bulk write fail, the Java Reactive Streams driver reports the errors only after attempting all operations.

Note

Unordered bulk operations do not guarantee order of execution. The order can differ from the way you list them to optimize the runtime.

To learn how to perform individual write operations, see the following guides:

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Delete