Mouli
Mouli

Reputation: 91

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

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.

Upvotes: 0

Views: 9861

Answers (2)

Mouli
Mouli

Reputation: 91

I have finally achieved it using the CompletableFuture of Java 8 like below:

  public CompletableFuture<List<Document>> getMongoDocuments() throws InterruptedException, ExecutionException {
    CompletableFuture<List<Document>> future = new CompletableFuture<>();
    List<Document> list = new ArrayList<>();

    collection.find().forEach((document) -> {
      try {
        list.add(document);
      } catch (Exception e) {
        LOGGER.error("Error while parsing document::" + document.toString(), e);
      }

    }, (final Void result, final Throwable t) -> {
      future.complete(list);
    });

    List<Document> resultList = future.get(); //Just for testing if everything is as planned
    LOGGER.info("getHighResDocumentsByDriveSessionVinAndLogDate:: Count::" + resultList.size());
    return future;
  }

Best Regards, Chandra.

Upvotes: 0

Jaipal
Jaipal

Reputation: 167

You can get back to the results by declaring the list outside the call.

For Example:

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

Since these operations are asynchronous,You need to make your method to wait until it gets completed.

I prefer you to go through this link

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

Upvotes: 2

Related Questions