mongodb 11000 error code code example

Example: mongo error 11000

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:

Tags:

Misc Example