Spring Boot MongoDB Database Example

Spring Boot made it easy to setup and configure projects, and develop applications, because spring boot takes care of adding dependent jars to projects and it provides default configuration.

In this tutorial, let’s see how to develop web application with MongoDB as database using spring boot by creating expense tracker web application which allows user to add, delete and view expenses. Expenses data is stored in MongoDB.


First create maven project in eclipse, for detailed steps for creating spring boot project, please see spring boot example.

Then add spring boot starters to pom. To include jars for spring mvc web application, we need to add spring-boot-starter-web starter and for including MongoDB dependencies, add spring-boot-starter-data-mongodb starter. Then do maven project update by right clicking project and clicking maven and project update.

<project xmlns=""






Let’s create entity class and define fields for capturing expense data. Let’s mark bill id with annotation ID.


public class Bill {
	private Long id;
	private String name;
	private String type;
	private Double amount;

	public Bill(Long id, String name, String type, Double amount) { = id; = name;
		this.type = type;
		this.amount = amount;

	public Long getId() {
		return id;

	public void setId(Long id) { = id;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getType() {
		return type;

	public void setType(String type) {
		this.type = type;

	public Double getAmount() {
		return amount;

	public void setAmount(Double amount) {
		this.amount = amount;


MongoDB Repository

It is easy to access MongoDB for read and write operation using spring data. Spring data defines MongoDB repository interface which you can extend to define find methods. Spring data generates code for saving, querying and deleting entity.


public interface MongoDbRepository extends MongoRepository<Bill, Long>{
	public List<Bill> findByType(String type);	


Let’s create a controller with add, find and delete expenses methods, map them to path and annotate the controller with @RestController. For enabling spring boot auto configuration, annotate the controller with @EnableAutoConfiguration annotation. Then define mongo repository type field in controller and annotate it with Autowired so spring will provide an instance of it and assign to it.

The mongoDB repository instance is used for inserting, reading, and deleting expenses data.

public class MongoApp 
	private MongoDbRepository mongoDbRepository;
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	public static void main(String[] args) {, args);

	public String home() {
		StringBuilder sb = new StringBuilder();
		sb.append("add bill url : /add/billId/name/type/amount");
		sb.append("delete bill url : /delete/billId");
		sb.append("get bill url : /get/billId");
		sb.append("get bills by type url : /getByType/type");
		return sb.toString();
	public String addBill(@PathVariable Long id, @PathVariable String name, 
			@PathVariable String type, @PathVariable double amount) {"insert bill"); Bill(id, name, type, amount));
		return "bill inserted";
	public List<Bill> getBillsByType(@PathVariable String type) {"get bills");		
		return mongoDbRepository.findByType(type);
	public Optional<Bill> getBill(@PathVariable Long id) {"get bills by type");
		return mongoDbRepository.findById(id);
	public String deleteBill(@PathVariable Long id) {"delete bill");
		return "bill deleted";

Project Structure

spring boot MongoDB example project

MongoDB Setup

For testing the application, we need MongoDB database. Let’s use free tier MongoDB database on cloud for testing our application. For detailed steps on how to use MongoDB on cloud, please see connecting to MongoDB database on cloud from applications tutorial.


If you install and start MongoDB on your local machine, you don’t need to add any configuration in order for the app to connect to MongoDB as spring boot provides default configuration.

But to inform spring boot to connect to MongoDB on cloud, we need to add connection string to configuration. After you setup MongoDB on cloud and get connection string, place it in file sand save it under src/main/resources folder. The property that needs be used for this is


Start the application by running the main method in controller, it will start the embedded tomcat server and deploy the web app. Access the application from browser to add, find and delete expenses.

spring boot mongo database examplespring boot mongo database query example