diff options
Diffstat (limited to 'jstests/noPassthrough/merge_on_secondary.js')
-rw-r--r-- | jstests/noPassthrough/merge_on_secondary.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/jstests/noPassthrough/merge_on_secondary.js b/jstests/noPassthrough/merge_on_secondary.js new file mode 100644 index 00000000000..94ecd8cc232 --- /dev/null +++ b/jstests/noPassthrough/merge_on_secondary.js @@ -0,0 +1,48 @@ +/** + * Tests the behavior of $merge being run on a secondary. + * + * @tags: [assumes_unsharded_collection, requires_replication, requires_spawning_own_processes] + */ +(function() { +"use strict"; + +load("jstests/aggregation/extras/merge_helpers.js"); // For withEachMergeMode. + +let replTest = new ReplSetTest({nodes: 2}); +replTest.startSet(); +replTest.initiate(); +replTest.awaitReplication(); + +let primary = replTest.getPrimary().getDB('test'); +let secondary = replTest.getSecondary().getDB('test'); +secondary.setSlaveOk(true); + +const collPrimary = primary.getCollection('coll'); +const collSecondary = secondary.getCollection('coll'); +const outColl = primary.getCollection('outColl'); + +assert.commandWorked(collPrimary.insert({_id: 0, a: 1}, {writeConcern: {w: 2}})); +assert.commandWorked(collPrimary.insert({_id: 1, a: 2}, {writeConcern: {w: 2}})); + +// Make sure the $merge succeeds without any duplicate keys. +withEachMergeMode(({whenMatchedMode, whenNotMatchedMode}) => { + // Skip the combination of merge modes which will fail depending on the contents of the + // source and target collection, as this will cause the aggregation to fail. + if (whenMatchedMode == "fail" || whenNotMatchedMode == "fail") { + return; + } + + collSecondary.aggregate([{ + $merge: { + into: outColl.getName(), + whenMatched: whenMatchedMode, + whenNotMatched: whenNotMatchedMode + } + }]); + + assert.eq(whenNotMatchedMode == "discard" ? 0 : 2, outColl.find().itcount()); + outColl.drop(); +}); + +replTest.stopSet(); +})();
\ No newline at end of file |