Spring Data Reactive MongoDB — CRUD Operations

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 🙂

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

BEAUTY MASTER — FLUTTER APP SETUP GUIDE

Recoil.js & simple global state

Charles Described 1983 Royal Tour of Australia with Diana as ‘Great Joy’

How we built BeoNews by using EXPO

Valid Anagram

What is JSON? (For beginners)

3 Ways to Improve Type Safety in Jest Tests

Use ESNext to write less JavaScript code!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinoth Selvaraj

Vinoth Selvaraj

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

More from Medium

Creating your first Spring Boot application

Spring Boot featured image

How to enable logging MongoDB queries for Spring Boot applications

Validating MongoDB Documents in Spring Boot

A MongoDB collection with a basic JSON schema

Custom Spring boot security with multiple profiles