[英]Execute sequencial queries in Spring Data Mongodb Reactive
I have to found All documents in collection B not referenced in collection A.我必须找到集合 B 中未在集合 A 中引用的所有文档。
Using the method in ReactiveMongoRepository使用 ReactiveMongoRepository 中的方法
Flux<A> findAll();
So I made this code:所以我做了这个代码:
collectionARepository.findAll()
.flatMap(d -> d.documentBID())
.collectList()
.flatMap(list -> collectionBRepository.findDocsNotIn(list))
.flatMap(b -> log.info(b.id())
.subscribe();
But, if there's no documents in collection A, the second query will not be executed.但是,如果集合 A 中没有文档,则不会执行第二个查询。 How can I force the execution of second to get all documents.
如何强制执行 second 以获取所有文件。
Found the solution.找到了解决办法。
My IDE for some reason was not show error, but When I use collectList
I passed from Flux to Mono.由于某种原因,我的 IDE 没有显示错误,但是当我使用
collectList
我从 Flux 传递到 Mono。 When I call flatMap (It should show an error) I cannot return a Flux, only a Mono.当我调用 flatMap (它应该显示错误)时,我无法返回 Flux,只能返回 Mono。 That causes an Exception and It was treat like "Entity not found".
这会导致异常,并且被视为“未找到实体”。 My bad.
我的错。
So here is my final code:所以这是我的最终代码:
collectionARepository.findAll()
.flatMap(d -> d.documentBID())
.collectList()
.flatMapMany(list -> collectionBRepository.findDocsNotIn(list))
.flatMap(b -> log.info(b.id())
.subscribe();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.