Spring boot MongoDB Tutorial

In this article, we are going to see how we can perform crud operations with Spring Boot and MongoDB

Creating spring boot application

The first step is to create a spring boot application we MongoDB dependency. For creating a spring boot application using the spring boot initializer and all required dependencies with MongoDB dependencies for spring boot and also add dependencies for lombok.

Spring Boot MongoDB dependency


After creating the spring boot project with all required dependencies. We are first going to create a mongo repository but we need a model class. So first create a model class. Our model class is about student and we store basic details of student.

Model Class

As our model class is for the student, so we will make the student id as primary key and roll no is unique. for making roll no unique in MongoDB with spring boot we need to use @Indexed(unique=true) with spring.data.mongodb.auto-index-creation=true in application.properties file and @Id will work for primary key


package com.gangforcode.MongoDBSpringBoot.model;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

public class Student {
    private String studentId;
    @Indexed(unique = true)
    private int rollNo;
    private String name;
    private String grade;

MongoDB Repository

Let’s create a mongo repository by extending MongoRepository. And declare a method findByRollNo in this.


package com.gangforcode.MongoDBSpringBoot.repo;

import com.gangforcode.MongoDBSpringBoot.model.Student;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface Repository extends MongoRepository<Student,String> {

MongoDB configuration

Add the following lines in the application.properties file


You can specify database name as you want.

now everything is completed, now we have to write the logic for the crud operation. Here we will write all the logic in the controller but you can create a service class if you want.


Now create a controller and inside the controller auto wire the repository.

package com.gangforcode.MongoDBSpringBoot.controller;

import com.gangforcode.MongoDBSpringBoot.repo.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class Controller {
private Repository repository;

Add Student

first logic we are going to write for adding a student; Add following code in controller here we directly adding the student details into database, but if you want you can validate the details of student.

public Student add(@RequestBody Student student)
Student std = repository.save(student);
return std;
} catch (Exception e) {
throw new RuntimeException(e);


Get All Student

public List<Student> findAll()
    return repository.findAll();

Get Student by roll no.

public List<Student> findAll()
return repository.findAll();

Update Student Details

public Student update(@PathVariable String studentId , @RequestBody Student student)
Optional<Student> std = repository.findById(studentId);
Student update = std.get();
return repository.save(update);
return null;

Delete Student

public String delete(@PathVariable String studentId)
Optional<Student> student = repository.findById(studentId);
if(student.isPresent()) {
return "successfully deleted";
return "Already Deleted";

2 thoughts on “Spring boot MongoDB Tutorial”

Leave a Comment