Welcome to Spring Data MongoDB example. Spring Data MongoDB is one of the Spring projects for integrating Spring Framework with most widely used NoSQL database MongoDB.

Spring Data MongoDB

One of the key benefit of using Spring is that it provides integration with most of the major frameworks that are used in enterprise application. For example, Spring ORM Hibernate Integration.

We will use latest version of Spring Framework and Spring Data MongoDB for our example project. Our final Spring Data MongoDB example project will look like below image.


Spring Data MongoDB can be used in a simple application too, it’s not required to use Spring framework with it. Let’s see this with a simple Spring MongoDB example. For that all you need to include below dependencies in pom.xml file, it will automatically include the compatible MongoDB java driver through maven transitive dependencies.

Spring Data MongoDB Example – Model Bean

We will have a simple model bean with some variables to be stored in MongoDB database.


It’s a simple java bean, however there are few important points that you should know.

  1. We know that every document in MongoDB is required to have a primary key with name _id, we can either provide it or MongoDB will generate it for us. We can use org.springframework.data.annotation.Id annotation with a model bean variable to map it to _id field.
  2. If the field name is “id” then we don’t need to use the @Id annotation, however it’s best practice to use it. In above class, we could have skipped @Id annotation.
  3. You should always have id field in the bean, otherwise it will not be mapped to any of the properties of the object and you will loose the primary key reference.

Now let’s see how we can easily use Spring Data MongoDB to perform CRUD operations on MongoDB database.


Now when I run above Spring Data MongoDB example program, it generates following output.

We can conclude following points for Spring Data MongoDB from our learning till now.

  1. Spring Data MongoDB provides wrapper over the MongoDB java driver, internally it’s using MongoDB java driver to perform database operations.
  2. MongoOperations declares a lot of methods for different operations and most of the time, they are sufficient for us. MongoTemplate is the implementation class and it requires Mongo or MongoClient (for newer MongoDB java driver versions) or MongoDbFactory to initialize it. We also need to provide the database name which will be used.
  3. If database is password protected, we can use org.springframework.data.authentication.UserCredentials to pass the authentication username and password details.
  4. org.springframework.data.mongodb.core.query.Query and org.springframework.data.mongodb.core.query.Criteria classes are used to define the query used to find particular record or records.
  5. The major benefit of Spring Data MongoDB is that we don’t need to worry about the conversion of java bean to Mongo DBObject and vice versa, as we saw in MongoDB Java Example.

Now let’s move forward to use Spring Data MongoDB in Spring environment. It’s very simple and mostly requires configuration related code that we can do through XML, annotations or through java config. However I will use XML based configuration for Spring Data MongoDB example.

Here is my final pom.xml with Spring Framework and Spring Data MongoDB dependencies.

Spring Data MongoDB DAO Classes

We will use DAO pattern for exposing different operations that can be performed on Person object.


Below is the MongoDB specific implementation class.


The code is pretty straight forward, so I won’t explain about them in detail.

Spring Data MongoDB Bean Configuration File

As always, the most important part of this application will be the spring bean configuration file. We will inject dependencies into different beans and define them.

Here is our final spring bean configuration file.


The important configurations that should be present are – Spring Data MongoDB schema and Mongo instance for MongoDB connection. I have defined MongoDbFactory instance for my convenience, we could also define MongoTemplate bean like below,

Spring Data MongoDB Test Program

Finally, let’s write a simple test program and run some CRUD operations on MongoDB database.


Now when I run above application, it generates following output.

Notice that when Spring context is closed, it’s taking care of closing the MongoDB connections too, so we don’t need to worry about that.

Also I am providing MongoDB Collection name in each of the queries, we can skip that if the collection name confirms to java naming convention. For example, for “Person” and “PersonAddress” objects default collection name used by Spring MongoDB would be “person” and “personAddress” respectively.

We can also use org.springframework.data.mongodb.core.mapping.Document annotation with Model class to define the collection name to be used for saving the document.

Spring Data MongoDB Annotation Based Configuration

If you want to use annotation based configuration, below Configuration class can be used for reference.


We would need other configurations too to inject MongoTemplate bean into our DAO implementation class, I am leaving that part to you.

Using MongoOptions for MongoDB Connection Options

We can use MongoOptions to define MongoDB options in spring bean configuration file like below. There are some other configuration options too, that you can check for optimizing your connections.

Spring Data MongoDB Example Summary

I hope this tutorial was good enough to get you started with Spring Data MongoDB, it’s not feasible to cover everything. You should look more into Query, Criteria and MongoTemplate methods to learn more. You can download the final project from below link.

By admin

Leave a Reply

%d bloggers like this: