Bulk write operations in Java MongoDB

MongoDB is a popular NoSQL database that allows for efficient and convenient data storage and retrieval. One of the key features of MongoDB is the ability to perform bulk write operations, which allow you to insert, update, and delete multiple documents in a single operation. In this article, we will explore how to perform bulk write operations in Java using the MongoDB Java driver.

Prerequisites

To follow along with the examples in this article, you will need to have the following prerequisites:

Bulk Insert

Bulk insert is useful when you need to insert a large number of documents into a collection. Instead of inserting each document individually, you can use bulk insert to insert them in batches, which can significantly improve performance.

import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.InsertOneModel;
import java.util.ArrayList;
import java.util.List;

public class BulkInsertExample {
    public static void main(String[] args) {
        // Get the MongoCollection instance
        MongoCollection<Document> collection = getMongoCollection();

        // Create a list to hold the insert models
        List<InsertOneModel<Document>> insertModels = new ArrayList<>();

        // Add documents to the list
        insertModels.add(new InsertOneModel<>(new Document("name", "John")));
        insertModels.add(new InsertOneModel<>(new Document("name", "Jane")));
        insertModels.add(new InsertOneModel<>(new Document("name", "Alice")));

        // Perform the bulk insert operation
        BulkWriteResult result = collection.bulkWrite(insertModels);

        // Print the number of inserted documents
        System.out.println("Number of documents inserted: " + result.getInsertedCount());
    }
}

In the above example, we create a list of InsertOneModel objects, each containing a document to be inserted. We then pass this list to the bulkWrite method of the MongoCollection object to perform the bulk insert operation. The BulkWriteResult instance returned by bulkWrite provides information about the operation, such as the number of inserted documents.

Bulk Update

Bulk update is useful when you need to update multiple documents that match a specific criteria. Instead of updating each document individually, you can use bulk update to update them in a single operation.

import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateManyModel;
import com.mongodb.client.model.Updates;

public class BulkUpdateExample {
    public static void main(String[] args) {
        // Get the MongoCollection instance
        MongoCollection<Document> collection = getMongoCollection();

        // Create the update model
        UpdateManyModel<Document> updateModel = new UpdateManyModel<>(
                Filters.eq("status", "pending"),
                Updates.set("status", "completed")
        );

        // Perform the bulk update operation
        BulkWriteResult result = collection.bulkWrite(Collections.singletonList(updateModel));

        // Print the number of updated documents
        System.out.println("Number of documents updated: " + result.getModifiedCount());
    }
}

In the above example, we create an UpdateManyModel object that specifies the filter criteria (Filters.eq) and the update operation (Updates.set). We then pass this model as a singleton list to the bulkWrite method of the MongoCollection object to perform the bulk update operation. The BulkWriteResult instance returned by bulkWrite provides information about the operation, such as the number of updated documents.

Bulk Delete

Bulk delete is useful when you need to delete multiple documents that match a specific criteria. Instead of deleting each document individually, you can use bulk delete to delete them in a single operation.

import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.DeleteManyModel;

public class BulkDeleteExample {
    public static void main(String[] args) {
        // Get the MongoCollection instance
        MongoCollection<Document> collection = getMongoCollection();

        // Create the delete model
        DeleteManyModel<Document> deleteModel = new DeleteManyModel<>(
                Filters.eq("status", "completed")
        );

        // Perform the bulk delete operation
        BulkWriteResult result = collection.bulkWrite(Collections.singletonList(deleteModel));

        // Print the number of deleted documents
        System.out.println("Number of documents deleted: " + result.getDeletedCount());
    }
}

In the above example, we create a DeleteManyModel object that specifies the filter criteria (Filters.eq). We then pass this model as a singleton list to the bulkWrite method of the MongoCollection object to perform the bulk delete operation. The BulkWriteResult instance returned by bulkWrite provides information about the operation, such as the number of deleted documents.

Conclusion

Bulk write operations in MongoDB are a powerful feature that can greatly improve the performance of your applications when dealing with large datasets. In this article, we explored how to perform bulk insert, update, and delete operations in Java using the MongoDB Java driver. By utilizing these bulk operations, you can optimize your code and enhance the overall efficiency of your application.

References