EVENTGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50! Learn more >

Spring Boot and MongoDB

In this tutorial we'll use Spring Boot to access data from a MongoDB Atlas cluster. To follow along you'll need to sign in to MongoDB Atlas.
Sign up for Atlas
Sign in

Java developers often make use of the Spring framework. Spring Boot allows developers to create microservices and web applications using Spring. By using this tool, you can rapidly create standalone applications without needing to make unnecessary configuration changes.

MongoDB is a great fit for Java developers who need a database. Combining Spring Boot and MongoDB results in applications that are fast, secure, reliable, and require minimal development time.

This tutorial shows how Spring Boot and MongoDB come together seamlessly with Spring Data MongoDB and will help you build a full Spring application.

Table of contents:

Spring Boot is an auto-configured microservice-based web framework that provides built-in features for security and database access.

With Spring boot, we can quickly create stand-alone applications without having to make too many configuration changes (as we will see later). MongoDB is the most popular NoSQL database because of the ease with which data can be stored and retrieved. Combining Spring Boot and MongoDB results in applications that are fast, secure, reliable, and require minimum development time.

This tutorial demonstrates how we can integrate Spring Boot with MongoDB using the Spring Data MongoDB API.

Getting started with MongoDB and Spring Boot

Spring is an application framework for Java, based on the MVC (Model-View-Controller) framework. Spring Boot is built on top of Spring and is mainly used for REST APIs. It has four layers:

  1. The presentation layer, for front end
  2. The business layer, for business logic and validation
  3. The persistence layer, for translating business objects to database objects
  4. The database layer, for handling CRUD operations

MongoDB can handle large amounts of structured and unstructured data, making it a database of choice for web applications. The Spring framework provides powerful connectors to easily perform database operations with MongoDB.

In this tutorial, we'll build a Spring Boot application, focusing on persistence and database layers. We'll run our program from our IDE to focus on CRUD operations. We'll add Spring Boot MongoDB configurations so that we can use Spring Boot with MongoDB.

What We Will Build

We're going to build a grocery list for a user. In this tutorial, we'll demonstrate the following:

  1. Creating a POJO (Plain Old Java Object) to represent a grocery item
  2. CRUD operations using MongoRepository
  3. An alternate approach for document updates using MongoTemplate.
What We Need

We'll need:

The complete code for this tutorial can be found on GitHub.

Getting started with Spring Initializr

Let's use Spring Initializr to generate a Spring Boot project. Using Spring Initializr takes care of creating a pom.xml file, which Maven uses for dependency management.

Select the following options:

Spring Initializr Project settings with Maven, Java, and select dependencies
  • Maven Project
  • Java language
  • Dependencies: Spring Web and Spring Data MongoDB

Enter the project metadata (as shown in the image above) and select the JAR option.

MongoDB model implementation

Our model is the POJO, or in this case, the GroceryItem class.

Let's create a package called com.example.mdbspringboot.model and add the class GroceryItem.java.

We use the annotation @Document to specify the collection name that will be used by the model. If the collection doesn't exist, MongoDB will create it.

If your IDE is Eclipse, you can use the Source -> Generate Getters and Setters option to create getters and setters for this code.

You'll note that in the above sample, the primary key in our MongoDB document is specified using the @Id annotation. If we don't do this, MongoDB will automatically generate an _id when creating the document.

Spring Boot MongoDB API implementation

The API implementation happens in the repository. It acts as a link between the model and the database and has all the methods for CRUD operations.

Let's create a package called com.example.mdbspringboot.repository to store all the repository files.

We first create an ItemRepository public interface, which extends the MongoRepository interface.

The first method, findItemByName, requires a parameter for the query, i.e., the field by which to filter the query. We specify this with the annotation @Query. The second method uses the category field to get all the items of a particular category. We only want to project the field's name and quantity in the query response, so we set those fields to 1. We reuse the method count() as it is.

MongoDB and Spring Boot CRUD examples

To connect to MongoDB Atlas, we specify the connection string in the application.properties file in the src/main/resources folder.

The connection string for a cluster can be found in the Atlas UI. There is no need to write connection-related code in any other file. Spring Boot takes care of the database connection for us.

We're also specifying the database name here. If it doesn't exist, MongoDB will create one.

In this Spring Boot MongoDB example, we are not using the Controller and the View. We will use a CommandLineRunner to view the output on the console.

Create the main application class MdbSpringBootApplication.java in the root package com.example.mdbspringboot:

Our class MdbSpringBootApplication implements the CommandLineRunner interface to run the Spring application. ItemRepository is Autowired, allowing Spring to find it automatically.

Spring initializes the Application Context using the @SpringBootApplication annotation. We also activate the Mongo Repositories using @EnableMongoRepositories. Our project structure should be similar to the below structure now:

project folder structure

Let’s now add the repository methods to the main class for CRUD operations:

Create operation using Spring Boot MongoDB

In order to create new documents, we will use the save method. The save method is available to us through the SimpleMongoRepository class, which implements the MongoRepository interface. Our ItemRepository interface extends MongoRepository.

MongoRepository structure

The save method will take a GroceryItem object as a parameter. Let's create five grocery items (documents) and save them using the save method in the following code:

Read operations using Spring Boot MongoDB

In this application, we perform four different read operations:

  • Fetching all the documents using findAll()
  • Getting a single document by name
  • Getting a list of items by category
  • Getting the count of items

We can create a helper method to display the output of read operations in a readable format:

Updates using Spring Boot MongoDB

To change the category from "snacks" to "munchies," we first need to find all documents with the category "snacks," set their categories to "munchies," then save all the modified documents.

Deletes using Spring Boot MongoDB

To remove an item from our grocery list, we can delete it by ID using deleteById.

To delete all the items, we can use the groceryItemRepo.deleteAll() method.

Putting it all together

Next, we implement the CommandLineRunner.run() method to call the above methods:

The output should be similar to the following:

Using MongoTemplate

To perform update operations using a particular field, we can also use the MongoTemplate class. The nice thing about MongoTemplate is that the update can be done in a single database interaction.

To use MongoTemplate, we create a custom repository where we build the update query.

Let's write a method to update the quantity of a grocery item.

Create an interface CustomItemRepository:

We can add as many methods as we need and provide the implementations in the CustomItemRepositoryImpl class:

Since MongoTemplate is @Autowired, Spring will inject the object dependency. The @Component annotation will allow Spring itself to detect the CustomItemRepository interface.

The next step is to call this method from our main class. We'll declare our customRepo similar to how we declared the groceryItemRepo:

And we'll need a method in our main class to call the customRepo method.

Add the above method in the run method to call it when the application is executed:

The resulting output should be:

In the MongoRepository example mentioned earlier, we had to do three operations (find, set, save). In this case, we updated in a single database transaction!

It's easy to connect MongoDB Atlas with Spring Boot

In this article, we've gone over the basic concepts of using Spring Boot with MongoDB and built a full Spring Boot application. To go beyond this Spring Boot starter, or to learn more about the core capabilities of Spring Data, refer to our handy guide.

Try this tutorial for yourself.

Sign up for Atlas

FAQ

How does MongoDB connect to Spring Boot?

MongoDB and Spring Boot interact using the MongoTemplate class and MongoRepository interface.

  • MongoTemplate: MongoTemplate implements a set of ready-to-use APIs. A good choice for operations like update, aggregations, and others, MongoTemplate offers finer control over custom queries.
  • MongoRepository: MongoRepository is used for basic queries that involve all or many fields of the document. Some examples include data creation, viewing documents, and more.

In either case, configuring Spring Boot with MongoDB only takes a few lines of code.

What is Spring Boot used for?

Spring Boot framework is used to create production-ready web applications with default configurations. Developers need not write extensive code. Spring Boot significantly reduces the development time. It automatically adds commonly used libraries for web applications, such as:

  • spring-webmvc.
  • tomcat.
  • validation-api.

Spring Boot also has embedded servlet container support. We can run Java programs as a standalone application by adding the spring-boot-starter-web dependency in pom.xml.

Where should I go from here?

Whether you're just learning Spring Boot or a Spring expert, MongoDB has you covered. To find more Spring Boot tutorials, information on the underlying Spring framework, or sample applications using Spring, refer to the following: