1
votes

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?

1
If you can, please don't code something in Java that you can do in SQL - its less performant by a huge factor, although it's more entertaining to write java.Frischling
It's a MongoDB database with no DBRef. How would u do that without Java?Monta
Ah, overread the "Mongo" part, sorry for that; I also posted an answer, since I guessed that you need the Java part anyways.Frischling

1 Answers

2
votes

Your problem is the collectList() part, since it makes a Mono from your Flux, which you obviously don't want - you're making a stream from the list immediately after collection. This here should do the same, plus mapping the Mono to a Flux with flatMapMany:

raceParticipationRepository.findByUserId(userId, status,
            PageRequest.of(page, 6))
      .map(ParticipationRepository::getRaceId)
                .collectList(Collectors.toList()) // returns Mono<List>
      .flatMapMany(list -> raceRepository.findAllById(list)); // Mono -> Flux again