VOOZH about

URL: https://www.geeksforgeeks.org/springboot/spring-boot-integration-with-mongodb-as-a-maven-project/

⇱ Spring Boot Integration With MongoDB as a Maven Project - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot Integration With MongoDB as a Maven Project

Last Updated : 27 Apr, 2026

MongoDB is a popular NoSQL database that stores data in JSON-like documents instead of traditional tables and rows. It is flexible and widely used in modern applications because it does not require a fixed schema. Spring Boot provides easy integration with MongoDB using Spring Data MongoDB, which simplifies database access and reduces boilerplate code.

  • Supports dynamic schema, allowing easy modification of data structure without affecting existing data.
  • Spring Boot enables quick setup and seamless integration with MongoDB using auto-configuration and starter dependencies.

Prerequisites

Steps To Implements MongoDB with Spring Boot

Step 1: Create a Spring Boot Project

Create a new project using Spring Initializr.

Project Configuration

  • Project: Maven
  • Language: Java
  • Spring Boot Version: Latest stable version
  • Group: com.gfg
  • Artifact: springBoot-MongoDB-SampleProject
  • Packaging: Jar
  • Java Version: 17 or higher

Add Dependencies:

Select the following dependencies:

  • Spring Web
  • Spring Data MongoDB

Download the project, extract it, and open it in your IDE.

👁 out

Step 2: Project Structure

After creating the project, the folder structure will look like this:

👁 Project Structure
 

As it is a maven project, let's start with adding dependencies via 

pom.xml

File to mention the connectivity with MongoDB database

Step 3: Add MongoDB Configuration

Open application.properties and configure the MongoDB connection.application.properties

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=geeksforgeeks

This configuration connects the Spring Boot application to the MongoDB database.

Step 4: Create the MongoDB Document Class

Create a class Book.java inside the docs package.

Book.java

Step 5: Create the MongoDB Document Class

Create a class Book.java inside the docs package.

BookRepository.java

Step 6: Create Service Layer

Create BookService.java inside the service package.

BookService.java

Step 7: Create Controller Class

Create BookController.java inside the controller package.

BookController.java

Main file that contains the main method and can be used to run as a Java application 

Step 8: Build the Maven Project

Run the following command:

mvn clean install # As this is maven project

This will compile the project and download dependencies.

Step 9: Run the Application

Run the Spring Boot application using:

mvn spring-boot:run

Output:

👁 Spring Boot Integration With MongoDB as a Maven Project Output

Step 10: Test the APIs

We can test the same in the below ways. First, let us add the book

URL: http://localhost:8080/addBook?bookId=1&isbnNumber=12345&bookName=JavaBasics&category=Programming

👁 Image

Now list out the books

URL: http://localhost:8080/getAllBooks

👁 Image

Let's add 2 more books

👁 Image

We can search books by means of category 

URL: http://localhost:8080/getBook?category=Programming

👁 Image

Similarly, we can do it by bookId as well

👁 Image

Like this, we can do all business logic as per our needs. It is quite easier to integrate MongoDB with SpringBoot technologies.

Comment
Article Tags:

Explore