MongoDB is a document-oriented NoSQL database that provides, high performance, high availability, and easy scalability. In Spring applications we could integrate with MongoDB using Spring Data dedicated interface. This tutorial will cover an introduction to Spring Data for MongoDB, we will present how to configure the environment to start working with that database and show common operations.
2. MongoDB configuration
Since Spring Boot can deduce the application's configuration from the JAR dependencies on the CLASSPATH all we need to do is to add entry (with spring-boot-starter-data-mongodb) in our pom.xml file:
By default, the Spring Boot tries to connect to a MongoDB server at mongodb://localhost/test, we could change that and other settings as well in the application.properties file:
In case our MongoDB instance is secured we must specify authentication-database and password in order to log in properly.
3. Run MongoDB instance
The easiest way to start an instance of MongoDB database on our local environment is to use docker:
Let me explain briefly used parameters:
docker run - command that runs the specified image and starts the docker container,
-d - parameter runs the container in background,
-name mongo-on-docker - defines a name for the container, so that we could easily found it,
-p 27888:27017 - the local port 27888 is mapped to the internal 27017 port (format local:internal),
Let's start with sample model object that will become MongoDB document:
As you can see this is a simple POJO class, in addition, we could use @Document annotation in case we want to name differently the collection in the Mongo database. By default, the collection will be named with a class name.
The @Id annotation is an object's unique identifier. If this value is empty Mongo will generate a random value after saving the object. If a value is set database will replace it or create new, depending on whether an object with the same id already exists in the Mongo or not.
The @Indexed annotation is used to support the efficient execution of queries in Mongo. If we want to use a field very often in search queries we should consider adding @Indexed annotation for that field.
5. Mongo Repository
To create a DAO object that will be responsible for CRUD operations on the MongoDB we will use MongoRepository:
This example code shows how to create a repository with CRUD operations for the Snippet object.
6. Insert object in MongoDB
To save new object in the Mongo database we could use insert(...) or save(...) method available in MongoRepository interface:
In this case, Snippet id field will be filled with auto-generated random value after the insert(...) method.
7. Update inserted object
To update an object stored in the Mongo database we could simply use save(...) method but in this case updated object needs to have a filled identifier.
8. Remove object
The MongoRepository provides two methods to delete object from MongoDB: deleteById(...) and delete(...). The first one takes object identifier and the second one expects an object as argument:
9. Found object by id
We could find objects using their identifier:
10. Find all inserted objects
There is also a method that returns all inserted values:
11. Get the first page
We could use special interface Pageable to return just a single page of data:
12. Check if an object exists in the database
Mongo repository allows checking if the object exists in the database:
13. Sorting collection
To sort values we could use findAll(...) method with given Sort instance:
14. JUnit test with all test cases
The JUnit test class with all the above examples could look like the following:
15. Conclusion
In this article, we presented how to configure, integrate and start using Spring Data for the Mongo database.
As usual, the code used in this article could be found on our GitHub.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}