MongoDB difference between error code 11000 and 11001

The code 11001 does not exist in the 2.5/2.6 branch on GitHub, so if you're trying a 2.5 version than you can't create it. I did have a look at the code, but I can't find any path that shows the 11001 code either directly.

The following few lines will show code 11001:

db.so.drop();
db.so.insert( { foo: 5 } );
db.so.ensureIndex( { foo: 1 }, { unique: true } );
db.so.insert( { foo: 6 } );

The expected 11000:

db.so.insert( { foo: 5 } );
E11000 duplicate key error index: test.so.$foo_1  dup key: { : 5.0 }

And now to reach the 11001:

db.so.insert( { foo: 6 } );
db.so.update( { foo: 6 }, { $set: { foo: 5 } } );
E11000 duplicate key error index: test.so.$foo_1  dup key: { : 5.0 }

Still the original 11000, but:

db.getPrevError();
{
    "err" : "E11000 duplicate key error index: test.so.$foo_1  dup key: { : 5.0 }",
    "code" : 11001,
    "n" : 0,
    "nPrev" : 1,
    "ok" : 1
}

That the original textual error message shows E11000 is a bug: https://jira.mongodb.org/browse/SERVER-5978


Mongo has an ErrorCategory enum which creates a DUPLICATE_KEY_ERROR for the following error codes:

private static final List<Integer> DUPLICATE_KEY_ERROR_CODES = Arrays.asList(11000, 11001, 12582);

The above is from mongodb java driver 3.6.4.

So both refer to duplicate keys.

Tags:

Mongodb