diff options
author | Hana Pearlman <hana.pearlman@mongodb.com> | 2021-11-22 13:20:41 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-11-22 13:45:50 +0000 |
commit | 08ed50a5e31392a9bf90c5d450941ff9c281740b (patch) | |
tree | d37dc47d572c8de21d2f3937328bbc081a311a5f /jstests/aggregation | |
parent | 7a9101652b4de3b4ef4f57c84e68d09eae793413 (diff) | |
download | mongo-08ed50a5e31392a9bf90c5d450941ff9c281740b.tar.gz |
SERVER-59132: Improve error message for $merge with whenMatched: fail
Diffstat (limited to 'jstests/aggregation')
-rw-r--r-- | jstests/aggregation/sources/merge/mode_fail_insert.js | 38 | ||||
-rw-r--r-- | jstests/aggregation/sources/merge/mode_replace_insert.js | 13 |
2 files changed, 44 insertions, 7 deletions
diff --git a/jstests/aggregation/sources/merge/mode_fail_insert.js b/jstests/aggregation/sources/merge/mode_fail_insert.js index 5ebc31ecbce..692d437263c 100644 --- a/jstests/aggregation/sources/merge/mode_fail_insert.js +++ b/jstests/aggregation/sources/merge/mode_fail_insert.js @@ -102,6 +102,44 @@ assert.doesNotThrow(() => coll.aggregate([ assert.eq(1, targetColl.find().itcount()); // +// Tests that a $merge aggregation fails with an error message indicating the duplicate key error +// occured during the $merge. +// +coll.drop(); +assert.commandWorked(coll.insert({a: 0, b: {c: 2}})); +dropWithoutImplicitRecreate(targetColl.getName()); +assert.commandWorked(targetColl.createIndex({a: 1, "b.c": 1}, {unique: true})); +assert.commandWorked(targetColl.insert({_id: 1, a: 0, b: {c: 2}})); + +// This time we should fail due to a collision on the "on" fields. +let res = assert.throws(() => coll.aggregate({ + $merge: { + into: targetColl.getName(), + whenMatched: "fail", + whenNotMatched: "insert", + on: ["a", "b.c"] + } +})); +assert.commandFailedWithCode(res, ErrorCodes.DuplicateKey); +assert.includes( + res.message, + "$merge with whenMatched: fail found an existing document with the same values for the 'on' fields"); + +// This time we should fail due to a collision on the "_id" field. +coll.drop(); +assert.commandWorked(coll.insert({_id: 1, a: 3, b: {c: 4}})); +res = assert.throws(() => coll.aggregate({ + $merge: { + into: targetColl.getName(), + whenMatched: "fail", + whenNotMatched: "insert", + on: ["a", "b.c"] + } +})); +assert.commandFailedWithCode(res, ErrorCodes.DuplicateKey); +assert.includes(res.message, "$merge failed due to a DuplicateKey error"); + +// // Tests for $merge to a database that differs from the aggregation database. // const foreignDb = db.getSiblingDB("merge_insert_only_foreign"); diff --git a/jstests/aggregation/sources/merge/mode_replace_insert.js b/jstests/aggregation/sources/merge/mode_replace_insert.js index 1975365ae06..80bee8f464a 100644 --- a/jstests/aggregation/sources/merge/mode_replace_insert.js +++ b/jstests/aggregation/sources/merge/mode_replace_insert.js @@ -130,13 +130,12 @@ assert.commandWorked(coll.insert([{_id: 0}, {_id: 1}])); dropWithoutImplicitRecreate(outColl.getName()); assert.commandWorked(outColl.createIndex({a: 1}, {unique: true})); -assertErrorCode( - coll, - [ - {$addFields: {a: 0}}, - {$merge: {into: outColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}} - ], - ErrorCodes.DuplicateKey); +const res = assert.throws(() => coll.aggregate([ + {$addFields: {a: 0}}, + {$merge: {into: outColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}} +])); +assert.commandFailedWithCode(res, ErrorCodes.DuplicateKey); +assert.includes(res.message, "$merge failed due to a DuplicateKey error"); // Test that $merge fails if the "on" fields contains an array. coll.drop(); |