While inserting multiple documents at once in MongoDB using collection.insertMany() function.
How can we handle failure in between the transaction e.g. if connection breaks during the insertion and few records are inserted? How to get the inserted records and non-inserted ones?
I read that atomicity is not supported when dealing with multiple documents, how can I ensure that full data is inserted?
Hi Abhishek,
How to get the inserted records and non-inserted ones?
By default, insertMany() inserts in order. If there is an error during the write, the driver would throw MongoBulkWriteException, which will tell you at what point does the insert fail.
For example, consider this snippet:
List<Document> documents = Arrays.asList(
new Document("_id", 0),
new Document("_id", 1),
new Document("_id", 2),
new Document("_id", 2),
new Document("_id", 3)
);
try {
collection.drop();
collection.insertMany(documents);
} catch (MongoBulkWriteException e) {
System.out.println(e);
}
Note that since _id must be unique, the insert will fail at the 3rd entry in the list. Running the code will provide:
com.mongodb.MongoBulkWriteException: Bulk write operation error on server localhost:27017. Write errors: [BulkWriteError{index=3, code=11000, message='E11000 duplicate key error collection: test.test index: _id_ dup key: { : 2 }', details={ }}].
The BulkWriteError entry contains index=3, which is the entry where it fails. Note that you cannot have the same information when you specify unordered inserts.
Having said that, insertMany() is one of many commands that are retryable. If you’re using MongoDB 4.0 and the latest driver, this command could be automatically retried if you enable retryable writes.
Best regards,
Kevin