Getting list of Documents into Java List using Mongo DB Async Driver

281 views
Skip to first unread message

Chandramouli Putta

unread,
May 10, 2016, 3:17:09 AM5/10/16
to mongodb-user

Hello All,


I am a newbie to the MongoDB. There was a suggestion to use the MongoDB Async Java Driver API instead of Spring-Data/Mongo DB Driver API since the async API supports the callbacks and non-blocking calls to the DB. While I was going through the below links I have noticed few differences.

Async Driver API: http://mongodb.github.io/mongo-java-driver/3.0/driver-async/reference/crud/Sync Driver API: http://mongodb.github.io/mongo-java-driver/3.0/driver/reference/crud/

The main difference of my concern is, how can we get the resultset documents into an arraylist/linkedlist using the async driver api. The async api page gives the below code block to traverse through the results, but not assigning them into a list of our choice:

// find documents
collection.find().into(new ArrayList<Document>(), 
    new SingleResultCallback<List<Document>>() {
        @Override
        public void onResult(final List<Document> result, final Throwable t) {
            System.out.println("Found Documents: #" + result.size());
        }
    });

This copies the documents into the new ArrayList (the first argument of into method), but there is no way to get it back.

While the sync api supports the operation like below, which copies all the result documents into an arraylist.

// find documents
List<BasicDBObject> foundDocument = collection.find().into(new ArrayList<BasicDBObject>());

Is the Async API still evolving or I am missing something? Are there any utilities available specifically for async driver api Inputs are greatly appreciated.


Best Regards, Chandra.

Ross Lawley

unread,
May 10, 2016, 4:48:04 AM5/10/16
to mongod...@googlegroups.com
Hi Chandra,

The Async api mirrors the sync API, however as operations are non blocking the results are passed to the callback at some point in the future - once the operation has completed.  With the into() method you can supply a list from outside the scope of the method call eg:

List<Document> myList = new ArrayList<Document>();
collection.find().into(myList
  new SingleResultCallback<List<Document>>() {
    @Override
    public void onResult(final List<Document> result, final Throwable t) {
        System.out.println("Operation Finished and myList now contains the results");
    }
});

The callback is completed once the into method has finished and can return the filled collection containing the query results.  From the example above you can see that the `myList` variable will eventually contain all the results, so you can use it outside the bounds of the callback. As this is a non blocking method care should be taken to ensure that callback has completed before trying to use `myList`.

I hope that helps,

Ross




--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/28fcf2e7-7cc7-42e5-8038-71d70d7708e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


{ name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0", "@MongoDB"],
  facebook :"MongoDB"}

Reply all
Reply to author
Forward
0 new messages