StackTips
 9 minutes

Performing MongoDB CRUD Operation in Spring Boot

By Nilanchala @nilan, On Sep 17, 2023 MongoDBSpring Boot 2.68K Views

MongoDB is a cross-platform document-oriented database program. It is classified as a NoSQL database, which means that it does not use the traditional SQL relational database model. Instead, MongoDB stores data in flexible, JSON-like documents, which can have fields that hold values of different data types.

The key advantages of using MongoDB are:

  1. Scalability: MongoDB can be easily scaled horizontally by adding more nodes to a replica set or sharded cluster, which enables it to handle increasing amounts of data and read/write loads.
  1. Flexibility: MongoDB's document data model allows you to store data in a flexible, JSON-like format, which makes it easier to change the structure of your data as your application evolves.
  1. Performance: MongoDB's use of indexing, sharding, and in-memory storage options can significantly improve the performance of your applications compared to traditional relational databases.
  1. Easy to use: MongoDB provides a simple and straightforward API for inserting, updating, and retrieving data, making it easier for developers to work with compared to traditional relational databases.
  1. High availability: MongoDB provides built-in high availability features, such as replica sets and auto-electing primary nodes, which can help ensure that your data is always available even in the event of a node failure.
  1. Agile development: MongoDB's flexible data model and easy-to-use API can help speed up the development process and reduce the time and effort required to build complex applications.
  1. Cost-effective: MongoDB is an open-source software, which means that you can use it for free. Additionally, it's scalable and flexible architecture can help reduce the costs associated with managing and storing data.

Overall, MongoDB is a highly flexible and scalable NoSQL database that provides a number of advantages over traditional relational databases, making it an excellent choice for a wide range of applications and use cases.

Spring Boot CRUD with MongoDB

Here is an example of a simple Spring Boot application that performs CRUD operations on a MongoDB database:

Adding MongoDB Spring Boot Dependency

Start by creating a new Spring Boot project and adding the following dependencies to your pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

MongoDB Connection Properties

In application.properties file add the following properties for mongodb connection

spring.data.mongodb.uri=mongodb://<host>:<port>/<database>
spring.data.mongodb.username=<username>
spring.data.mongodb.password=<password>

Data Model Class Declaration

Create a model class, for example Person which will be mapped to a MongoDB collection. This class should have fields that correspond to the fields in the MongoDB document, as well as appropriate getters and setters.

@Document(collection = "people")
public class Person {
    @Id
    private String id;
    private String name;
    private int age;

    //getters and setters
}

Mongo Repository Interface

Create a MongoRepository interface to perform CRUD operations on the Person class.

public interface PersonRepository extends MongoRepository<Person, String> {

}

Using Mongo Repository From Services

In your application main class, autowire the PersonRepository and use it to perform CRUD operations. For example:

@Service
class PersonService {

	@Autowired
	private PersonRepository repository;

	public void addPerson(Person person) {
	    repository.save(person);
	}

	public List<Person> getAllPeople() {
	    return repository.findAll();
	}
}

This is just an example, and you will likely need to add more functionality to your application depending on your specific requirements.

Please note that this is just a starting point and you should consult the official Spring Boot and MongoDB documentation for more information on how to properly implement your application.

References

Spring Boot MongoDB References – MongoDB Reference

nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.