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 do 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>?
Thank you very much in advance
collection.find().forEach(...String documentsAsJson = collection.find().map(Document::toJson)
.into(new ArrayList<>())
.stream()
.collect(Collectors.joining(", ", "[", "]"));
public String getDocuments(){
...
collection.find(query).map(Document::toJson)
.into(new HashSet<String>(), new SingleResultCallback<HashSet<String>>() {
@Override
public void onResult(HashSet<String> strings, Throwable throwable) {
// HERE I HAVE TO GET THE ALL DATA IN THE SET
// AND MAKE IT TO A JSON STRING
// AFTER THAT I WOULD WAKE THE MAIN THREAD
}
});
// HERE I HAVE TO PUT THE MAIN THREAD TO WAIT UNTIL I HAVE THE DATA IN THE onResult()
// SO I CAN RETURN THE DATA BACK TO THE FRONT-END
return jsonString;
}