}
public CompletableFuture<T> insert(T elem) {
CompletableFuture<T> future = new CompletableFuture<>();
try {
Document doc = Document.valueOf(mapper.writeValueAsString(elem));
collection.insert(doc).register((result, e) -> {
if (result != null && result.wasAcknowledged()) {
elem.setId(doc.getObjectId("_id"));
future.complete(elem);
} else {
logger.error("Error while creating a new document in insert() : " + doc.toString(), e);
future.cancel(true);
}
});
} catch (JsonProcessingException e) {
logger.error("Error while creating element " + elem.toString() + " in insert()", e);