在 Flux 中使用 Mono 结果

Zon*_*Zon 3 java project-reactor spring-webflux

PROBLEM方法需要等待Mono操作结果,在Flux操作中使用它并返回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;
}
Run Code Online (Sandbox Code Playgroud)

研究 我希望该dateTimeMono流由 Mongo 反应式驱动程序订阅和终止,所以我不订阅。如果我使用Mono.zipMono<Flux> 作为返回类型。

任务 如何等待dateTimeMono值,在 Flux 操作中使用它并从中获取 Flux?

JEY*_*JEY 5

您应该使用flaMapMany

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