I'm using Spring 5 webflux with ReactiveMongoRepositories. Suppose I have 2 collections "Race" and "RaceParticipation":
Race : {
"_id" : "ID",
"name" : "Name"
}
RaceParticipation : {
"_id" : "ID",
"userId" : "ID",
"raceId" : "ID"
}
I need to find a page of Races in which a given user has participated.
The solution I went with is call raceParticipationRepository.findByUserId("userId", PageRequest.of(0,10))
This returns a Flux<UserParticipation>
.
The I should collect the ids from the response and call raceRepository.findAllById(ids)
.
Here is a snippet:
Flux<Race> findUserRaces(String userId, int page){
return raceParticipationRepository.findByUserId(userId, status,
PageRequest.of(page, 6))
.collectList()
.map(list -> list.stream()
.map(ParticipationRepository::getRaceId)
.collect(Collectors.toList()))
.flatMap(list -> raceRepository.findAllById(list));
}
This code doesn't compile. "Required Flux<Race> but flatMap was inferred to Mono..."
Any idea on what I'm missing or how should I do?