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.
xxxxxxxxxx
@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
public class FreelancerService {
@Autowired
private FreelancerRepository repository;
public Flux<Freelancer> findBySkillsOne(final List<String> skills){
return this.repository.findBySkillsIn(skills);
}
public Flux<Freelancer> findBySkillsAll(final List<String> skills){
return this.repository.findBySkillsAll(skills);
}
public Mono<Freelancer> getPerson(final String id){
return this.repository.findById(id);
}
public Mono<Freelancer> savePerson(final Freelancer person){
return this.repository.save(person);
}
public Mono<Freelancer> updatePerson(final Freelancer person){
return this.repository.findById(person.getId())
.map(p -> person)
.flatMap(this.repository::save);
}
public Mono<Void> deletePerson(final String id){
return this.repository.deleteById(id);
}
}
https://vinsguru.medium.com/spring-data-reactive-mongodb-crud-operations-c3985736ed0d