Implementing Bulk Writes using Spring Data for MongoDB
Rate this tutorial
BulkOperations is an interface that contains a list of write operations to be applied to the database. They can be any combination of
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.
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.
We will implement a class that holds the configuration to the MongoClient object that the Spring Data framework will utilize.
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 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.
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:
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