I am using the Mongo DB java driver:
collection.bulkWrite(documents);
I have 1 million records to be inserted. If the insertion of one of the records fails then on the first failure the remaining records won't get inserted.
In order to avoid this I found that there are BulkWriteOptions
with ordered
as false
;
collection.bulkWrite(documents, new BulkWriteOptions().ordered(false) )
If any exception occurs during the above operation, can we get the list of the records for which bulkwrite
failed and can we try inserting those records again?
I think you're looking for something similar..
BulkWriteOptions bulkWriteOptions = new BulkWriteOptions();
bulkWriteOptions.ordered(true);
BulkWriteResult bulkWriteResult = null;
try {
bulkWriteResult = mongoCollection.bulkWrite(updateDocuments,
bulkWriteOptions);
} catch (BulkWriteException e) {
List<BulkWriteError> bulkWriteErrors = e.getWriteErrors();
for (BulkWriteError bulkWriteError : bulkWriteErrors) {
int failedIndex = bulkWriteError.getIndex();
Long failedEntity = entityList.get(failedIndex);
System.out.println("Failed record: " + failedEntity);
//handle rollback
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With