Mongodb Async vs Sync Driver for Java

I am very confused about java drivers for Mongodb. Reading the official documentation, it seems that you can use the regular MondoDB driver or the Async MongoDB driver.

First question: Can I use both in one application or do I need to choose one?

Trying to use the Async driver, I found things that I used (with a regular driver) in which I got a little lost. For example, I used 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 to a json string, since they did not include the class JSONfrom the Async driver. Second question: If I cannot use both drivers at the same time, how can I serialize FindIterable<Document>?

+4
source share
1 answer

Answers:

  • 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 for which you need a response from MongoDB (e.g. find ()). And you will use the Async driver for those that you really don't need, for fire and forget actions (like an insert or update).
  • Thus, the question of serialization gets the answer from the foregoing. If you get a response, you are using a synchronization driver, so you can continue to use the class JSON:

JSON.serialize(iterable);

+1
source

Source: https://habr.com/ru/post/1611970/


All Articles