Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB difference between error code 11000 and 11001

Tags:

mongodb

According to this HIGHLY incomplete list http://www.mongodb.org/about/contributors/error-codes/ they're both related to duplicate keys. But I was not able to get a 11001 error. All of the following threw a 11000 error:

  • inserting a document with an _id that already existed
  • inserting a document with duplicate fields where the fields had a compound unique index
  • updating a document with said compound unique index

So this goes completely against the linked page, which says 11000 is for _id and 11001 would occur on updates (not inserts).

So my question is: When does 11001 occur?

like image 783
Prinzhorn Avatar asked Aug 03 '13 12:08

Prinzhorn


People also ask

What is code 11000 in MongoDB?

The error E11000 duplicate key error ... { person_name: 'Carl-Fredrik Linder' } is saying that the aggregation query is trying to create a document with person_name: 'Carl-Fredrik Linder' more than once - that is violating the unique index constraint on the person_name in the persons collection.

What is code 11000?

The Current Procedural Terminology (CPT®) code 11000 as maintained by American Medical Association, is a medical procedural code under the range - Debridement Procedures on the Skin.


2 Answers

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

like image 103
Derick Avatar answered Sep 22 '22 17:09

Derick


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.

like image 20
nimrodm Avatar answered Sep 23 '22 17:09

nimrodm