1

我有一堆,DocumentCollection找回所有这些。这是我的情况:

  1. 我正在使用 Java Reactive Streams 驱动程序
  2. 我正在使用将CodecRegistry我的Document反序列化为我的Pojo

问题是该find()方法的所有风格都返回 aFindPublisher<Pojo>并且不用说任何类型的值发射都会导致Pojo对象的返回。我想要一个List<Pojo>或一个Set<Pojo>退货。如何返回 aList<Pojo或 a Set<Pojo>

quickstart中,他们使用find().first()which 返回单个对象Document,因此单个Pojo对象是有意义的。没有返回多个的示例Document

4

1 回答 1

0

使用MongoDB Reactive Streams DriverRxJava,例如:

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));
于 2020-12-17T08:11:05.503 回答