summaryrefslogtreecommitdiff
path: root/jstests/aggregation
diff options
context:
space:
mode:
authorMihai Andrei <mihai.andrei@10gen.com>2020-04-03 16:45:02 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-04-06 13:58:41 +0000
commitadc802c85945cd3029d991c7ce82e3bdaa31384e (patch)
tree60a5893b16ae899d1c000a03946bdf09d6ee9d32 /jstests/aggregation
parent3c79139cb6a4e7d281ab9a1025182761b3134e41 (diff)
downloadmongo-adc802c85945cd3029d991c7ce82e3bdaa31384e.tar.gz
SERVER-47335 Guarantee document ordering with $sort stage in batch_writes.js to trigger expected DuplicateKeyError
(cherry picked from commit b4210c3633fc7157014a494e27928994313af860)
Diffstat (limited to 'jstests/aggregation')
-rw-r--r--jstests/aggregation/sources/merge/batch_writes.js10
1 files changed, 9 insertions, 1 deletions
diff --git a/jstests/aggregation/sources/merge/batch_writes.js b/jstests/aggregation/sources/merge/batch_writes.js
index 1d0c5502391..8fa0b232fbc 100644
--- a/jstests/aggregation/sources/merge/batch_writes.js
+++ b/jstests/aggregation/sources/merge/batch_writes.js
@@ -61,9 +61,17 @@ assert.soon(() => {
return outColl.find().itcount() == 9;
});
+// Note that a $sort comes before the $merge to guarantee that {_id: 1, a: 1} will always be
+// seen before {_id: 2, a: 2}. If {_id: 1, a: 1} is seen first, it gets inserted and will
+// trigger a DuplicateKeyError since a's value is duplicated by {_id: 2, a: 1}. If {_id: 2, a:
+// 2} is seen first, then it will make the replacement {_id: 2, a: 1} => {_id: 2, a: 2},
+// preventing a duplicate key error from arising later on.
assertErrorCode(
coll,
- [{$merge: {into: outColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}}],
+ [
+ {$sort: {a: 1}},
+ {$merge: {into: outColl.getName(), whenMatched: "replace", whenNotMatched: "insert"}}
+ ],
ErrorCodes.DuplicateKey);
assert.soon(() => {
return outColl.find().itcount() == 9;