MongoDB Developer
Java
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
Javachevron-right

Implementing Bulk Writes using Spring Data for MongoDB

Published Aug 10, 2022 • Updated Aug 11, 2022
SpringJava
FULL APPLICATION
Copy Link
facebook icontwitter iconlinkedin icon
random alt
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.

Copy Link
facebook icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

How to migrate from Realm Java SDK to Realm Kotlin SDK


May 09, 2022
Tutorial

Single-Collection Designs in MongoDB with Spring Data (Part 1)


Oct 14, 2022
Tutorial

How to Update Realm SDK Database Schema for Android


Sep 02, 2022
Quickstart

Java - Aggregation Pipeline


Sep 23, 2022
Table of Contents