MongoDB is one of the popular document-based No-SQL databases used for developing web applications. Usage of NoSQL database instead of the traditional databases, provides advantages like improved performance, efficiency, etc.

In this post, we will learn how to perform CRUD operation by using Spring boot application with MongoDB database.

We will use

  • Spring boot – Version 2.2.4.RELEASE
  • Lombok
  • Java version 1.8
  • MongoDB(For the Windows)

We will install MongoDB on the Windows system in the next step.

Set up MongoDB database on Windows

In this section, we will set up the MongoDB database and the required tool to browse the database.

Set up MongoDB database

Download the MongoDB database from the MongoDB website. Extract the downloaded zip file. Navigate to the /bin directory, as shown below.

MongoDB installation Windows

To start the MongoDB server, open the command prompt and type ‘mongod’, as show in the below image.

MongoDB installation Windows

Once the MongoDB server starts successfully, we should be able to see that the server is listening on default port number 27017.

MongoDB installation Windows

In case of any error, make sure that the directory structure /data/db present under the current directory.

Set up MongoDB compass

To browse the MongoDB database, we have to download the MongoDB compass. Extract the downloaded zip file, navigate inside the extracted folder, and run the MongoDBCompass.exe file.

Spring Boot MongoDB CRUD example

Enter the MongoDB connection string as shown in the new connection dialog, and click on the connect button.

Spring Boot MongoDB CRUD example

Create a new database by clicking on the Create database button with the name School with a collection name called Student.

Spring Boot MongoDB CRUD example

We have our MongoDB database setup ready. Now, we can create our Spring boot application.

Create the Spring boot application

Create the Spring boot application with the required dependencies. Add spring-boot-starter-web, spring-boot-starter-data-mongodb and lombok dependencies.

The spring-boot-starter-data-mongodb library gives the required support for connecting the spring boot application with the MongoDB database.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot</description>





Add the MongoDB connection string to the created Spring boot application’s file.

Create the Student class

Create a class and add the required fields, as shown below.

Annotate the class with the annotation @Document. This annotation specifies this class as a MongoDB document.

We have defined the ‘id’ field with string datatype. This will automatically assign an auto generated value to the ‘id’ field.

package com.asb.example.model;


import lombok.Getter;
import lombok.Setter;

public class Student {

	private String id;
	private String name;
	private String grade;

Create the repository

Create a new repository interface with the name and extend the MongoRepository repository. This is similar to the JPA repository, where we extend the JpaRepository interface.

package com.asb.example.repository;


import com.asb.example.model.Student;

public interface StudentRepository extends MongoRepository<Student, String> {


Create the service layer

Create a class and add the methods to support the CRUD operation on the Student document.

package com.asb.example.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.asb.example.model.Student;
import com.asb.example.repository.StudentRepository;

public class StudentService {

	private StudentRepository studentRepository;
	public List<Student> getStudents() {
		return studentRepository.findAll();
	public Student saveStudent(Student student) {
	public Student updateStudent(Student student) {
	public String getStudent(String id) {
		return "Student with id: " + id + " deleted successfully";

Create the resource layer

Create a REST controller class called and add the required method for CRUD operations.

package com.asb.example.resource;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.asb.example.model.Student;
import com.asb.example.service.StudentService;

public class StudentController {

	private StudentService studentService;

	public ResponseEntity<List<Student>> getStudents() {
		List<Student> students = studentService.getStudents();
		return new ResponseEntity<>(students, HttpStatus.OK);

	public ResponseEntity<Student> saveStudent(@RequestBody Student student) {
		Student s = studentService.saveStudent(student);
		return new ResponseEntity<>(s, HttpStatus.CREATED);

	public ResponseEntity<Student> updateStudent(@RequestBody Student student) {
		Student s = studentService.updateStudent(student);
		return new ResponseEntity<>(s, HttpStatus.CREATED);

	public ResponseEntity<String> deleteStudent(@RequestParam(name = "id") String id) {
		String message = studentService.getStudent(id);
		return new ResponseEntity<>(message, HttpStatus.OK);

We have successfully created REST endpoints using Spring boot and MongoDB to perform CRUD operation on the Student object. that gets stored in the MongoDB database. 🙂

The application

Create an object of the student by sending the required values on the postman, as shown below.

Spring Boot MongoDB CRUD example

We can observe the created object of Student in the MongoDB database.

MongoDB compass

Retrieve the student details by using the GET endpoint.

Spring Boot MongoDB CRUD example

Update the student details with the help of the PUT endpoint.

Spring Boot MongoDB CRUD example

Delete the Student object by sending the ‘id’ request parameter.

Spring Boot MongoDB CRUD example


In this post, we learned how to perform CRUD operation with Spring boot MongoDB application. We also learned how to set up the MongoDB database on the Windows system.

Example source code is available on GitHub. Happy coding 🙂

You may also interested in