Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb Async vs Sync Java driver

I´m quite confused about java drivers for Mongodb. Reading the official documentation it seems that you can use the normal MondoDB Driver or the MongoDB Async Driver.

The first question is: Can I use both in the same application or I have to choose one?

Trying to use the Async driver I found things that I used to do (with the normal driver) in which I get a bit lost now. For example, I used to do this:

FindIterable<Document> iterable = db.getCollection("my_coll").find(query);
String json = JSON.serialize(iterable);

And now I really don´t know how to convert the result into a json string since they have not included the JSON class from the Async driver. Second question: If I cannot use both drivers at the same time, how can I then serialize a FindIterable<Document>?

like image 681
ernirulez Avatar asked Oct 16 '15 15:10

ernirulez


1 Answers

The answers are:

  • Yes of course you can use both drivers. In fact, if you really care about performance in your application you should use the Sync driver for those actions that you need a response from MongoDB (like find()). And you will use the Async driver for the ones that you don't really need it, for "fire and forget" actions (like insert or update).
  • So the serialization question gets answered from the above. If you get a response you are using the sync driver, therefore you can keep using JSON class:

JSON.serialize(iterable);

like image 134
ernirulez Avatar answered Sep 23 '22 23:09

ernirulez