[英]MongoDb Java driver reactive with POJO mapping find List of documents in a type safe way
我在一個Collection
中有一堆Document
,並且想檢索所有這些文檔。 這是我的情況:
CodecRegistry
將我的Document
反序列化為我的Pojo
問題是find()
方法的所有風格都返回FindPublisher<Pojo>
並且不用說任何類型的值發射都會導致Pojo
object 返回。 我想要返回List<Pojo>
或Set<Pojo>
。 如何返回List<Pojo
或Set<Pojo>
?
在快速入門中,他們使用find().first()
返回單個Document
,因此單個Pojo
object 是有意義的。 沒有返回多個Document
的示例。
使用MongoDB Reactive Streams Driver和RxJava ,例如:
Publisher<Document> publisher = collection.find();
List<Document> docs = Flowable.fromPublisher(publisher)
.blockingStream()
.collect(Collectors.toList());
[編輯添加] 您可以使用非阻塞調用,例如:
List<Document> docs = new ArrayList<>();
Observable.fromPublisher(publisher).subscribe(doc -> docs.add(doc));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.