summaryrefslogtreecommitdiff
path: root/jstests/core/apply_ops2.js
blob: a4569bc2acb464546525d4bd28739ba7179843a3 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Test applyops upsert flag SERVER-7452
 *
 * The test runs commands that are not allowed with security token: applyOps.
 * @tags: [
 *   not_allowed_with_security_token,
 *   requires_non_retryable_commands,
 *   requires_fastcount,
 *   # 6.2 removes support for atomic applyOps
 *   requires_fcv_62,
 *   # applyOps is not supported on mongos
 *   assumes_against_mongod_not_mongos,
 *   # applyOps uses the oplog that require replication support
 *   requires_replication,
 *   # Tenant migrations don't support applyOps.
 *   tenant_migration_incompatible,
 * ]
 */

var t = db.apply_ops2;
t.drop();

assert.eq(0, t.find().count(), "test collection not empty");

t.insert({_id: 1, x: "init"});

// alwaysUpsert = true
print("Testing applyOps with alwaysUpsert = true");

var res = db.runCommand({
    applyOps: [
        {
            op: "u",
            ns: t.getFullName(),
            o2: {_id: 1},
            o: {$v: 2, diff: {u: {x: "upsert=true existing"}}}
        },
        {
            op: "u",
            ns: t.getFullName(),
            o2: {_id: 2},
            o: {$v: 2, diff: {u: {x: "upsert=true non-existing"}}}
        }
    ],
    alwaysUpsert: true
});

assert.eq(true, res.results[0], "upsert = true, existing doc update failed");
assert.eq(true, res.results[1], "upsert = true, nonexisting doc not upserted");
assert.eq(2, t.find().count(), "2 docs expected after upsert");

// alwaysUpsert = false
print("Testing applyOps with alwaysUpsert = false");

res = db.runCommand({
    applyOps: [
        {
            op: "u",
            ns: t.getFullName(),
            o2: {_id: 1},
            o: {$v: 2, diff: {u: {x: "upsert=false existing"}}}
        },

        {
            op: "u",
            ns: t.getFullName(),
            o2: {_id: 3},
            o: {$v: 2, diff: {u: {x: "upsert=false non-existing"}}}
        }

    ],
    alwaysUpsert: false
});

assert.eq(true, res.results[0], "upsert = true, existing doc expected to succeed, but it failed");
assert.eq(false, res.results[1], "upsert = false, nonexisting doc upserted");
assert.eq(2, t.find().count(), "2 docs expected after upsert failure");

// alwaysUpsert not specified, should default to true
print("Testing applyOps with default alwaysUpsert");

res = db.runCommand({
    applyOps: [
        {
            op: "u",
            ns: t.getFullName(),
            o2: {_id: 1},
            o: {$v: 2, diff: {u: {x: "upsert=default existing"}}}
        },

        {
            op: "u",
            ns: t.getFullName(),
            o2: {_id: 4},

            o: {$v: 2, diff: {u: {x: "upsert=default non-existing"}}}
        },
    ]
});

assert.eq(true, res.results[0], "default upsert, existing doc update failed");
assert.eq(true, res.results[1], "default upsert, nonexisting doc not upserted");
assert.eq(3, t.find().count(), "2 docs expected after upsert failure");