-
Type: Improvement
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
-
Labels:
-
Fully Compatible
When executing a $merge with a whenMatched: fail option specified, the error message can be misleading. See the following example:
> db.source.find() { "_id" : 1, "a" : 2, "b" : 3, "c" : 4 } { "_id" : 2, "a" : 3, "b" : 4, "c" : 5 } { "_id" : 3, "a" : 4, "b" : 5, "c" : 6 } > db.target.find() { "_id" : 1, "a" : 2, "black" : "sheep" } > db.source.aggregate([ ... { ... $merge: { ... into: "target", ... on: "a", ... whenMatched: "fail", ... whenNotMatched: "insert" ... } ... } ... ]) uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "E11000 duplicate key error collection: playground.target index: _id_ dup key: { _id: 1.0 }", "code" : 11000, "codeName" : "DuplicateKey", "keyPattern" : { "_id" : 1 }, "keyValue" : { "_id" : 1 } } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:618:17 assert.commandWorked@src/mongo/shell/assert.js:708:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12 @(shell):1:1
Instead of elucidating that the operation failed because a document in the source matched one in the target collection, a duplicate key error is returned.
The error message should be updated to indicate that this is expected due to the documents matching in this case.