The E11000 duplicate key exception contains wrong details which sometimes lead to the exception on the driver since we cannot parse it.
Consider the following queries:
// prepare collection db.coll.createIndex( { "testValue" : 1 }, { unique : true, collation: { locale : 'sv', strength : 1, numericOrdering : true } } ) { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "commitQuorum" : 1, "ok" : 1 }
Then, try to add two the same documents one by one:
- More simple case:
// 1 attempt db.coll.insertOne({ "testValue": "def"}) { "acknowledged" : true, "insertedId" : ObjectId("5e695401da7e6a4f1b712884") } // 2 attempt db.coll.insertOne({ "testValue": "def"}) {"t":{"$date":"2020-03-11T21:11:30.702Z"},"s":"E", "c":"QUERY", "id":0,"ctx":"js","msg":"{}","attr":{"message":"WriteError({\n\t\"index\" : 0,\n\t\"code\" : 11000,\n\t\"errmsg\" : \"E11000 duplicate key error collection: test.coll index: testValue_1 dup key: { testValue: \\\"/13\\\" }\",\n\t\"op\" : {\n\t\t\"_id\" : ObjectId(\"5e695402da7e6a4f1b712885\"),\n\t\t\"testValue\" : \"def\"\n\t}\n}) :\nWriteError({\n\t\"index\" : 0,\n\t\"code\" : 11000,\n\t\"errmsg\" : \"E11000 duplicate key error collection: test.coll index: testValue_1 dup key: { testValue: \\\"/13\\\" }\",\n\t\"op\" : {\n\t\t\"_id\" : ObjectId(\"5e695402da7e6a4f1b712885\"),\n\t\t\"testValue\" : \"def\"\n\t}\n})\nWriteError@src/mongo/shell/bulk_api.js:458:48\nmergeBatchResults@src/mongo/shell/bulk_api.js:855:49\nexecuteBatch@src/mongo/shell/bulk_api.js:919:13\nBulk/this.execute@src/mongo/shell/bulk_api.js:1163:21\nDBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:264:9\n@(shell):1:1"}}
This case contains only issue with wrong exception details:
testValue_1 dup key: { testValue: \\\"/13\\\" }
- Worse case:
// 1 attempt db.coll.insertOne({ "testValue": "abcdefghijkl123456789"}) { "acknowledged" : true, "insertedId" : ObjectId("5e695418da7e6a4f1b712886") } // 2 attempt db.coll.insertOne({ "testValue": "abcdefghijkl123456789"}) {"t":{"$date":"2020-03-11T21:11:53.807Z"},"s":"E", "c":"QUERY", "id":0,"ctx":"js","msg":"{}","attr":{"message":"WriteError({\n\t\"index\" : 0,\n\t\"code\" : 11000,\n\t\"errmsg\" : \"E11000 duplicate key error collection: test.coll index: testValue_1 dup key: { testValue: \\\")+-/13579;=?\\u000f�\\r9\\u000fe��\\\" }\",\n\t\"op\" : {\n\t\t\"_id\" : ObjectId(\"5e695419da7e6a4f1b712887\"),\n\t\t\"testValue\" : \"abcdefghijkl123456789\"\n\t}\n}) :\nWriteError({\n\t\"index\" : 0,\n\t\"code\" : 11000,\n\t\"errmsg\" : \"E11000 duplicate key error collection: test.coll index: testValue_1 dup key: { testValue: \\\")+-/13579;=?\\u000f�\\r9\\u000fe��\\\" }\",\n\t\"op\" : {\n\t\t\"_id\" : ObjectId(\"5e695419da7e6a4f1b712887\"),\n\t\t\"testValue\" : \"abcdefghijkl123456789\"\n\t}\n})\nWriteError@src/mongo/shell/bulk_api.js:458:48\nmergeBatchResults@src/mongo/shell/bulk_api.js:855:49\nexecuteBatch@src/mongo/shell/bulk_api.js:919:13\nBulk/this.execute@src/mongo/shell/bulk_api.js:1163:21\nDBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:264:9\n@(shell):1:1"}}
In this case we will see the wrong error part:
testValue_1 dup key: { testValue: \\\")+-/13579;=?\\u000f�\\r9\\u000fe��\\\" }
but the worst thing that we won't be able to parse this error on the driver side. And as a result this error will trigger a new deserialization exception which will fully hide the original one.
It looks like this ticket is related to https://jira.mongodb.org/browse/SERVER-26050
- is related to
-
SERVER-26050 Unique key violation for index with a non-simple collation has unclear error message
- Backlog
- related to
-
SERVER-60298 Explain can include ICU collation keys encoded as strings, which can be invalid UTF-8
- Closed
-
SERVER-50454 Avoiding sending the "keyValue" field to drivers on duplicate key error
- Closed