summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/merge_on_secondary.js
blob: 94ecd8cc232e4617adad0c46634af3f2dd6fd5b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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();
})();