How to limit number of results in ReactiveMongoRepository



I am looking for a way to pass the limit to the mongo query in ReactiveCrudRepository

I tried adding "First2" to the method name but I'm still getting all the results.

What I'm really looking for is a way to pass the value of the 'limit' to the method, passing it in request as @RequestParam int limit

This is my code for the repository

public interface ReactiveUserRepository
extends ReactiveCrudRepository<User, String> {

@Query("{ 'roles': ?0 }")
Flux<User> findFirst2ByRole(String role);

And this is controller method:

@GetMapping(path = "/byrole", produces = "application/stream+json")
Flux<User> getByRole(@RequestParam String role) {
return users.findFirst2ByRole(role).doOnNext(next -> {
System.out.println("Next user=" + next.getAssocId());
}).switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("No users found with role=%s", role))));

Continue reading...