Spring Data Reactive MongoDB — CRUD Operations

Vinoth Selvaraj
3 min readOct 28, 2020

In this article, I would like to show you the CRUD operations for a Spring Boot application using reactive mongodb driver.

Project Setup:

Lets first create a simple spring boot project with these dependencies.

Sample Application:

Our application is for freelancers to register themselves. So that people can search for the freelancers with specific skill sets.

MongoDB Setup:

I use docker-compose to set up mongodb.

version: "3"
services:
mongo:
image: mongo
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
mongo-express:
image: mongo-express
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password

Entity:

The entity class is designed as below.

@Data
@Document
@ToString
public class Freelancer {

@Id
private String id;
private String name;
private int age;
private List<String> skills;

}

Repository:

We create a repository to do basic CRUD operations by extending ReactiveMongoRepository. We also add couple of methods to the interface for search.

  • findBySkillsAll — We might want to search for people with all given skills.
  • findBySkillsIn — We might want to search for people containing 1 of the given skills.
@Repository
public interface FreelancerRepository extends ReactiveMongoRepository<Freelancer, String> {

@Query("{ 'skills': { $all: ?0 } }")
Flux<Freelancer> findBySkillsAll(List<String> skills);

Flux<Freelancer> findBySkillsIn(List<String> skills);

}

Service:

Lets create a service class for the CRUD operations and for the above search functionality.

RestController:

Lest create the controller as shown here. Do note that when we use reactive driver nothing happens until we subscribe to that action. If you notice, even a delete method which could have been ‘void’ return type returns a Mono<Void> .

  • I directly expose entity class for this demo. In real life you might want to use a DTO.

Application Properties:

spring.data.mongodb.database=admin
spring.data.mongodb.username=admin
spring.data.mongodb.password=password

Demo:

  • Lets start the application.
  • I register few freelancers as shown here using the POST endpoint.
{
"name": "sam",
"age": 40,
"skills": [ "js", "react", "python"]
}

{
"name": "jack",
"age": 38,
"skills": [ "js", "angular", "postgres"]
}

{
"name": "james",
"age": 30,
"skills": [ "java", "reactor", "mongo"]
}

{
"name": "smith",
"age": 32,
"skills": [ "qa", "selenium"]
}

We should be able to view all the records via Mongo express at port 8081.

  • GET
  • PUT
  • Search By All skills
  • Search By one of the skills
  • DEL

Summary:

We were able to do simple CRUD operations using Spring Data with reactive mongo driver easily as Spring Data does all the heavy lifting. You can find the source code here.

Happy learning 🙂

--

--

Vinoth Selvaraj

Principal Software Engineer — passionate about software architectural design, microservices.