[JAVA-1592] BulkWriteError.getDetails() seems to always return an empty DBObject Created: 11/Dec/14  Updated: 11/Sep/19  Resolved: 23/Jun/15

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Mark Helmstetter Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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);



 Comments   
Comment by Jeffrey Yemin [ 11/Dec/14 ]

Actually, looking at the code, the driver is taking the contents of the "errInfo" field for the details property. But again, the server doesn't always send one, and looking at the server code, it seems pretty rare that it ever would.

Comment by Jeffrey Yemin [ 11/Dec/14 ]

The details are just passed through from the server, so unless there is a bug it means that the server isn't sending any details. You can see this in the shell:

> db.test.runCommand({insert : 'test', documents : [{_id : 1}] })
{
	"ok" : 1,
	"n" : 0,
	"writeErrors" : [
		{
			"index" : 0,
			"code" : 11000,
			"errmsg" : "E11000 duplicate key error index: test.test.$_id_ dup key: { : 1.0 }"
		}
	]
}

There is no details document. The errmsg field from the response is available via com.mongodb.BulkWriteError#getMessage.

Generated at Thu Feb 08 08:55:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.