1

I have a bunch of Document in a Collection and would like to retrieve all of them. This is my situation:

  1. I am using the Java Reactive Streams driver
  2. I am using the CodecRegistry to get my Document deserialized to my Pojo

The problem is that all flavours of the find() method returns a FindPublisher<Pojo> and needlessly to say that any kind of value emission will result in the returning of Pojo object. I want a List<Pojo> or a Set<Pojo> returned. How do I return a List<Pojo or a Set<Pojo>?

In the quickstart, they are using the find().first() which returns a single Document and hence a single Pojo object makes sense. There is no example for returning multiple Document.

Shankha057
  • 1,296
  • 1
  • 20
  • 38
  • 1
    For clarification: Can you show how you are accessing the collection? If I use `database.getCollection("pojos", Pojo.class)`, then I can use `List pojos = new ArrayList<>();` followed by `collection.find().forEach(a -> pojos.add(a));`. This assumes my codec is set up correctly, of course. But my approach uses a `FindIterable` not a `FindPublisher` - so I may be missing the reactive piece of the puzzle. – andrewJames Dec 16 '20 at 20:53
  • @andrewjames I am aware of the `FindIterable` approach. But I am unable to find a `FindPublisher` approach example. If I have to go by the `findIterable` approach and adapt to `findPublisher` then it would be something like `Single.fromPublisher(collection.find()).map(pojo -> list.add(pojo)....)` and then return the `list` somehow. I am following the collection access setup as per the link provided in my question , but with the reactive streams driver. – Shankha057 Dec 17 '20 at 04:06

1 Answers1

0

Using MongoDB Reactive Streams Driver and RxJava, for example:

Publisher<Document> publisher = collection.find();
List<Document> docs = Flowable.fromPublisher(publisher)
                              .blockingStream()
                              .collect(Collectors.toList());

[EDIT ADD] You can use a non-blocking call, for example:

List<Document> docs = new ArrayList<>();
Observable.fromPublisher(publisher).subscribe(doc -> docs.add(doc));
prasad_
  • 12,755
  • 2
  • 24
  • 36
  • First of all, I want to keep it reactive. I don't want to block. That's the entire point of using the reactive streams driver. Secondly, there is no point using a backpressure aware construct like `Flowable` when you are going to block either way. Thirdly, there is no `blockingStream()` method for `Flowable` instance in RxJava 3. – Shankha057 Dec 17 '20 at 09:33
  • (1) You can use a non-blocking methods from the API (2) Method exists [RxJava3 Flowable#blockingStream()](http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Flowable.html#blockingStream--) (3) You can use _no backpressure_ [Observable](http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Observable.html) – prasad_ Dec 17 '20 at 09:50
  • 1. Please refer to the comments below the question to understand the actual issue. If I was getting a `Document` then I would be able to get the objects that I require. But I'm already mapped to a `Pojo` and that's what I get, not a `Document`. 2. I am using [io.reactivex.Flowable](http://reactivex.io/RxJava/javadoc/io/reactivex/Flowable.html) and not the ones that you are using. 3. Also, please refer to the comments under the question. That will help you get a better picture. – Shankha057 Dec 17 '20 at 11:08
  • You can use a Pojo object of your choice instead of a Document. – prasad_ Dec 17 '20 at 11:09
  • My `find()` returns `FindPublisher` and not `FindPublisher`. That is where I am having problems. Because even a `collection.find()` returns a `FindPublisher`. How am I supposed to interpret this? Also, I am returning something like `Single.fromPublisher(coll.find())....` directly – Shankha057 Dec 17 '20 at 11:15
  • _I am using io.reactivex.Flowable and not the ones that you are using._ I think you are using the RxJava 2 APis. The Observable methods I had shown in the examples are the same in both the versions. – prasad_ Dec 17 '20 at 11:18
  • I am using the the latest Micronaut release and they are using RxJava3 there – Shankha057 Dec 17 '20 at 11:34