-
Type:
Task
-
Resolution: Done
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
When executing a unordered bulk operation that throws BulkWriteException, the contained BulkWriteErrors seem to always be empty DBObjects.
The documentation is unclear on what we might expect in the details:
Gets the details associated with this error. This document will not be null, but may be empty.
It would be useful if the details contained the document/operation that failed. Since BulkWriteOperation doesn't seem to have any public accessors for the WriteRequests, the client would need to maintain his own indexed list of inserts in order to get details on failures.
Example:
MongoClient m = new MongoClient("localhost"); DB db = m.getDB( "test" ); DBCollection coll = db.getCollection( "bulk" ); coll.drop(); coll.createIndex(new BasicDBObject("i", 1), new BasicDBObject("unique", true)); BulkWriteOperation bulkWrite = coll.initializeUnorderedBulkOperation(); for (int i = 0; i < 100; i++) { bulkWrite.insert(new BasicDBObject("i", i)); } // Now add 10 documents to the batch that will generate a unique index error for (int i = 0; i < 10; i++) { bulkWrite.insert(new BasicDBObject("i", i)); } BulkWriteResult result = null; List<BulkWriteError> errors = null; try { result = bulkWrite.execute(); } catch (BulkWriteException bwe) { bwe.printStackTrace(); errors = bwe.getWriteErrors(); result = bwe.getWriteResult(); } for (BulkWriteError e : errors) { System.out.println(e.getIndex() + " failed"); // this is always an empty DBObject System.out.println(e.getDetails()); } System.out.println(result);