Introduction

In this guide, you'll learn how to get started with MongoDB and Spring Boot, a powerful framework for building Java applications. MongoDB is a popular NoSQL database known for its flexibility and scalability. We'll cover the basics of setting up a Spring Boot project, integrating MongoDB, and performing CRUD (Create, Read, Update, Delete) operations using the Spring Data MongoDB framework. Sample code and examples will help you understand the integration process.


Prerequisites

Before you begin, make sure you have the following prerequisites:

  • Java Development Kit (JDK) installed on your system.
  • Spring Boot project setup. You can create one using
    Spring Initializr
    or your preferred method.
  • MongoDB installed and running locally or accessible through a connection string.
  • An integrated development environment (IDE) for writing Java applications.

Step 1: Adding Spring Data MongoDB Dependency

Start by adding the Spring Data MongoDB dependency to your Spring Boot project. You can include it in your project's

pom.xml
or
build.gradle
file:

    org.springframework.boot    spring-boot-starter-data-mongodb
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

Step 2: Configuring MongoDB

Configure MongoDB connection properties in your application's

application.properties
or
application.yml
file:

# application.properties
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=mydb

Step 3: Creating a Model

Create a Java model to represent the data you want to store in MongoDB. For example, create a model for a "Product" entity:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "products")
public class Product {
@Id
private String id;
private String name;
private double price; // Getters and setters
}

Step 4: Implementing CRUD Operations

You can now perform CRUD operations on your MongoDB database using Spring Boot and Spring Data MongoDB. Here are some basic examples:

Create (Insert) Data

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public Product saveProduct(Product product) {
return productRepository.save(product);
}
}

Read (Query) Data

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface ProductRepository extends MongoRepository<Product, String> {
Page<Product> findAll(Pageable pageable);
}

Update Data

public class ProductService {
// ...
public Product updateProduct(String id, Product updatedProduct) {
// Check if the product with the given ID exists
if (productRepository.existsById(id)) {
updatedProduct.setId(id);
return productRepository.save(updatedProduct);
}
return null;
}
}

Delete Data

public class ProductService {
// ...
public void deleteProduct(String id) {
productRepository.deleteById(id);
}
}

Conclusion

You've successfully started working with MongoDB and Spring Boot. This guide covers the basics of setting up a Spring Boot project, configuring MongoDB, creating a model, and implementing CRUD operations using Spring Data MongoDB. With these foundational skills, you can explore more advanced Spring Boot and MongoDB features and build Java applications that interact with MongoDB databases.