30

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?

Prinzhorn
  • 22,120
  • 7
  • 61
  • 65
  • I can’t find error `11001` at https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.err and that link is dead. Is this question specific to a version of mongo? – binki May 29 '17 at 16:09

2 Answers2

35

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

Derick
  • 35,169
  • 5
  • 76
  • 99
  • Wow, thanks. I'm using 2.4.5 and it was in fact just the error message that was wrong (I didn't use `getPrevError`). Does the fact that you couldn't find any `11001` in the source mean they drop it after 2.4? Do you know any source (e.g. an issue) for this? – Prinzhorn Aug 05 '13 at 15:48
  • No, I couldn't find an issue for that, but I left a comment at that SERVER-5978 issue. – Derick Aug 05 '13 at 15:55
  • 1
    Thanks for your research. I'm gonna keep checking for both `11000` and `11001` in my client code. – Prinzhorn Aug 06 '13 at 05:44
  • 1
    Based on this answer I gather that the difference between 11000 and 11001 is that they're emitted for inserts and updates respectively? Or is this purely a server version difference? – bloudermilk Feb 18 '15 at 10:39
  • Why does the server emit different error codes for these cases? What is the best way to check for either error. Something like `(error.code & ~1) === 11000`? – binki May 29 '17 at 16:06
3

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.

nimrodm
  • 23,081
  • 7
  • 58
  • 59