在Flux中使用Mono结果。

3

问题:方法需要等待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;
}

研究 我希望Mongo reactive driver会订阅并终止dateTimeMono流,所以我不需要自己进行订阅。如果我使用Mono.zip,将会得到Mono<Flux>作为返回类型。

任务 如何等待dateTimeMono的值,将其用于Flux操作并从中获取Flux?


1
getDateTime().flatMapMany(date -> reactiveMongoTemplate.find(new Query(Criteria.where("dateTime").gt(date)),My.class,collectionName)) - JEY
问题是为什么你想这样做?Mono总是返回零或一个项目。 - Aris
是的,flatMapMany可以解决问题。非常感谢。你不想发表一个答案吗? - Zon
1个回答

5

你应该使用flatMapMany

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接