Use Mono Result in Flux

Viewed 522

PROBLEM Method needs to wait for Mono operation result, use it in Flux operation and return Flux.

public Flux<My> getMy() {
  Mono<ZonedDateTime> dateTimeMono = getDateTime();

  Flux<My> mies = reactiveMongoTemplate.find(
        new Query(Criteria.where("dateTime").gt(dateTimeMono)),
        My.class,
        collectionName);

  return mies;
}

RESEARCH I expect that dateTimeMono stream is subscribed and terminated by Mongo reactive driver, so I don't subscribe. If I use Mono.zip I get Mono<Flux> as return type.

TASKS How to wait for dateTimeMono value, use it in Flux operation and get Flux out of it?

1 Answers

You should use flaMapMany:

public Flux<My> getMy() {
    return getDateTime().flatMapMany(date -> reactiveMongoTemplate.find(new Query(Criteria.where("dateTime").gt(date)),My.class,collectionName));
}
Related