EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
Java
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
Javachevron-right

Implementing Bulk Writes using Spring Boot for MongoDB

3 min read • Published Aug 10, 2022 • Updated Mar 22, 2023
SpringJava
Facebook Icontwitter iconlinkedin icon
spring logo
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

The Spring Data Framework is used extensively in applications as it makes it easier to access different kinds of persistence stores. This article will show how to use Spring Data MongoDB to implement bulk insertions.
BulkOperations is an interface that contains a list of write operations to be applied to the database. They can be any combination of InsertOne, updateOne updateMany replaceOne deleteOne deleteMany
A bulkOperation can be ordered or unordered. Ordered operations will be applied sequentially and if an error is detected, will return with an error code. Unordered operations will be applied in parallel and are thus potentially faster, but it is the responsibility of the application to check if there were errors during the operations. For more information please refer to the bulk write operations section of the MongoDB documentation.

Getting started

A POM file will specify the version of Spring Data that the application will use. Care must be taken to use a version of Spring Data that utilizes a compatible version of the MongoDB Java Driver. You can verify this compatibility in the MongoDB Java API documentation.

Application class

The top level class is a SpringBootApplication that implements a CommandLineRunner , like so:
Now we need to write a few classes to implement our bulk insertion application.

Configuration class

We will implement a class that holds the configuration to the MongoClient object that the Spring Data framework will utilize.
The @Configuration annotation will allow us to retrieve values to configure access to the MongoDB Environment. For a good explanation of Java-based configuration see JavaConfig in the Spring reference documentation for more details.
In this implementation we are using a flag, mongodb.atlas, to indicate that this application will connect to Atlas. If the flag is false, an SSL Context may be created using a trustStore, This presents a certificate for the root certificate authority in the form of a truststore file pointed to by truststore.path, protected by a password (truststore.pwd) at the moment of creation. If needed the client can also offer a keystore file, but this is not implemented.
The parameter mongodb.uri should contain a valid MongoDB URI. The URI contains the hosts to which the client connects, the user credentials, etcetera.

The document class

The relationship between MongoDB collection and the documents that it contains is implemented via a class that is decorated by the @Document annotation. This class defines the fields of the documents and the annotation defines the name of the collection.
Setters and getters need to be defined for each field. The @Id annotation indicates our default index. If this field is not specified, MongoDB will assign an ObjectId value which will be unique.

Repository classes

The repository is implemented with two classes, one an interface and the other the implementation of the interface. The repository classes flesh out the interactions of the application with the database. A method in the repository is responsible for the bulk insertion:

Conclusion

In this example we are creating a random list of Products, which then are bulk-inserted unordered. We have specified a write concern of 1, which means that the server will acknowledge the operation once the Primary has written the operation in the journal.
This write concern results in faster insertions, but there is a chance of losing data if the Primary crashes or an election takes place and data has not yet been replicated. To avoid this danger, use write concern Majority.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Integrating MongoDB with Amazon Managed Streaming for Apache Kafka (MSK)


Jun 12, 2023 | 7 min read
Tutorial

Secure your API with Spring Data MongoDB and Microsoft EntraID


Apr 02, 2024 | 8 min read
Article

How Queryable Encryption Can Keep James Bond Safe


Apr 02, 2024 | 2 min read
Tutorial

Using Azure Kubernetes Services for Java Spring Boot Microservices


Apr 15, 2024 | 9 min read
Table of Contents