Process of integrating Spring boot and MongoDB

Posted By :Hemant Chauhan |30th September 2020

How to integrate Spring boot and mongodb


In this article, we will demonstrate how to connect Spring Data with Mongodb and create a small REST service with Spring Boot and MongoDB.

MongoDB is a open source and document-oriented NoSQL database that stores data in JSON form. The main advantage of using mongodb is it is used for high volume data storage.

The commonly used terms which we used in spring data mongodb are:


1. Database: It is container of collections like in RDBMS contains so many tables in it.

2. Collection: It is equivalent to tables in relational database(mysql).

3. Document: It is equivalent to rows in RDBMS. It is single record in collection.

4. Fields: It is equivalent to columns in RDBMS having a key value pair


Tools and technologies used


1. Firstly install mongodb in your local system according to OS.

2. JDK 8

3. Intellij

4. Maven is need to be installed in your system.

5. Postman which is need to hit the rest api.



There are two approaches through which we connect springboot java to MongoDB database:

1. Mongo Repository

2. Mongo Template


We will used Mongo Repository for integration. MongoRepository provide us common functionalities and it is similar to JPA Repository. It is easy to use and implement than mongo template.


Lets get started, we will make use of Spring Initializr tool for quickly setting up the project. We will use just one dependency as shown below:




Now we will create a simple model class of Users Information. We have a simple model class


 @Document(collection = "user")  
 public class User {  
 private String userId;  
 private String name;  
 private String email;  
 private long rollno;  
 ## Constructors  
 ## Setters and getters  
 ## tostring  


@Document annotation is used for creating and representing the collection name..It is used for the same purpose with @Entity annotation in JPA.

@Id is used for mark fields as identity purpose. It is used like we represent primary key in jpa.


Create a interface which extends MongoRepository. This will give us access to all the CRUD operations. Now you can easily add and remove element from your Mongodb collection.


 public interface UserRepository extends MongoRepository<User, String> {  
 Student findByRollNumber(long rollno);  
 Student findByEmail(String email);  


Now Setup a connection between mongo and springboot. To connect to the mongoDB database, go to the Resouces directory and edit or create file according to this given below:






Now, we create the User service. It contains four methods which is used for save, update, getall ,getbyelement and delete the users.


 public interface UserService {  
 List<User> findAll();  
 Student findByEmail(String email);  
 void saveOrUpdateUser(User user);  
 void deleteUser(String id);  


Create a service layer class


 public class UserServiceImpl implements UserService {  
 private UserRepository userRepository;  
 public List<User> findAll() {  
 return userRepository.findAll();  
 public User findByEmail(String email) {  
 return userRepository.findByEmail(email);  
 public void saveOrUpdateUser(User user) {;  
 public void deleteUser(String id) {  



Finally, we can create the REST controller


 public class UserController {  
 private UserService userService;  
 @GetMapping(value = "/")  
 public List<User> getAllUsers() {  
 return userService.findAll();  
 @GetMapping(value = "/byEmail/{email}")  
 public User getUserByEmail(@PathVariable("email") String email) {  
 return userService.findByEmail(email);  
 @PostMapping(value = "/save")  
 public ResponseEntity<?> saveOrUpdateUser(@RequestBody User user) {  
 return new ResponseEntity("User added successfully", HttpStatus.OK);  




About Author

Hemant Chauhan

He has a good understanding in java and have a strong willing to learn new technologies, likes to work on spring framework & mysql. He is good in sports.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Chat With Us