Overview about Spring Data MongoDB

The spring-data-mongodb library of Spring, in addition to providing us with the MongoOperations interface makes it easy to manipulate MongoDB, which also provides us with the mechanism that a Spring Data project needs to do: minimize the repetitive code involved dealing with data management systems when developing applications that use the Spring framework. How is it in details? Let’s find out in this tutorial!

First, I will create a Maven project as an example:

Overview about Spring Data MongoDB

with Spring Data MongoDB dependency as follows:

Spring Data MongoDB will use the MongoTemplate object to manage the connection to MongoDB so we need to initialize the bean for this object:

Since I use MongoDB local and do not need username, password, so as you see, I just initialized the simple MongoClient object as above.

As I said in Overview about Spring Data JPA about Spring Data, to minimize code, the Repository interface was introduced. Each of the modules in Spring Data will have other interfaces that extend from this interface to implement its own database system. Also, in Spring Data Common, we have two other interfaces that extend from the Repository interface, the CrudRepository and the PagingAndSortingRepository, which define operations such as create, read, update, delete, paging, sort.

In Spring Data Mongodb, as Spring Data Jpa, we have only one extend interface from the PagingAndSortingRepository interface, is MongoRepository interface. You can see this clearly in the following image:

Overview about Spring Data MongoDB

As you can see, the class implementing the MongoRepository interface is SimpleMongoRepository.

OK, now I’m going to do an example to see how the MongoRepository works!

I will create an interface named HelloRepository extends from MongoRepository with the following content:

The Student class has the following contents:

As you can see, using the MongoRepository interface, our Student object does not need to be defined as a Document anymore. Spring Data MongoDB will do it for us, the Student object is now just a POJO. Spring Data MongoDB will map the Student class to the collection named “student”. If you want to change the name of the collection, you can declare it for the Student class annotation @Document as follows:

In this tutorial, I will use the default collection name from the name of the Student class.

Now we declare the HelloRepository interface in the Spring container as follows:

Here, I use the mongo namespace:repositories with the base package com.huongdanjava.springdatamongodb to scan the HelloRepository interface. With this statement, Spring Data MongoDB automatically retrieves the implementation for HelloRepository interface, is SimpleMongoRepository object, as you see in the image above, to initialize the Spring container.

OK, here we have completed the necessary configurations. Now, let’s try it out.

Suppose in the database you have the following data:

Overview about Spring Data MongoDB

then when running the following example:

The result will be:

Overview about Spring Data MongoDB

There are many other operations that the MongoRepository interface supports:

Overview about Spring Data MongoDB

Please explore more according to your needs.

Add Comment