Reputation: 584
I am using BulkWriteWithOptions for inserting multiple documents in DB. I want the inserted documents so that I can know which one was inserted, failed or duplicate document. Following is the piece of code I am using
mongoClient.bulkWriteWithOptions(collection, operations, options, repoAsyncResult -> {
if (repoAsyncResult.failed()) {
LOGGER.error("Bulk insertion failed : {}", repoAsyncResult.cause().getMessage());
if(repoAsyncResult.cause() instanceof MongoBulkWriteException ){
MongoBulkWriteException exception = (MongoBulkWriteException)repoAsyncResult.cause() ;
exception.getWriteErrors().forEach(error -> {
LOGGER.error("Insert Error : " + error.getMessage());
});
}
repoFuture.fail(repoAsyncResult.cause());
} else {
LOGGER.info("Bulk insertion successful : {}", repoAsyncResult.result().toJson());
repoFuture.complete(repoAsyncResult.result().toJson());
}
});
Is there any way to get the inserted documents as a result?
Upvotes: 0
Views: 764
Reputation: 3073
No, you can only get the IDs of upserted documents from repoAsyncResult.result().getUpserts()
(a List<JsonObject>
whose .getAsString("_id")
will return the upserted IDs.)
Upvotes: 2