summaryrefslogtreecommitdiff
path: root/jstests/core/isolated.js
blob: 3bfbcae09c6c62c0f61b9d34ca24c0330d23325e (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// @tags: [requires_non_retryable_writes]

// Tests for the $isolated update/delete operator.
(function() {
    "use strict";

    const coll = db.isolated_test;
    coll.drop();

    const isMaster = db.runCommand("ismaster");
    assert.commandWorked(isMaster);
    const isMongos = (isMaster.msg === "isdbgrid");

    //
    // $isolated in aggregate $match stage.
    //

    assert.commandFailed(db.runCommand(
        {aggregate: coll.getName(), pipeline: [{$match: {$isolated: 1}}], cursor: {}}));

    //
    // $isolated in count.
    //

    assert.throws(function() {
        coll.find({$isolated: 1}).count();
    });

    //
    // $isolated in distinct.
    //

    assert.throws(function() {
        coll.distinct("a", {$isolated: 1});
    });

    //
    // $isolated in find.
    //

    // $isolated is not allowed in a query filter.
    assert.throws(function() {
        coll.find({$isolated: 1}).itcount();
    });

    // $isolated is not allowed in find with explain.
    assert.throws(function() {
        coll.find({$isolated: 1}).explain();
    });

    // $isolated is not allowed in $elemMatch projection.
    assert.writeOK(coll.insert({a: [{b: 5}]}));
    assert.throws(function() {
        coll.find({}, {a: {$elemMatch: {$isolated: 1}}}).itcount();
    });

    //
    // $isolated in findAndModify.
    //

    // $isolated is allowed in the query when upsert=false.
    assert.writeOK(coll.insert({_id: 0, a: 5}));
    assert.eq(
        {_id: 0, a: 5, b: 6},
        coll.findAndModify({query: {_id: 0, $isolated: 1}, update: {$set: {b: 6}}, new: true}));

    // $isolated is allowed in the query when upsert=true.
    assert.writeOK(coll.insert({_id: 1, a: 5}));
    assert.eq(
        {_id: 1, a: 5},
        coll.findAndModify({query: {_id: 1, $isolated: 1}, update: {$set: {b: 6}}, upsert: true}));

    // $isolated is not allowed in $pull filter.
    assert.writeOK(coll.insert({_id: 2, a: [{b: 5}]}));
    assert.throws(function() {
        coll.findAndModify({query: {_id: 2}, update: {$pull: {a: {$isolated: 1}}}});
    });

    // $isolated is not allowed in arrayFilters.
    if (db.getMongo().writeMode() === "commands") {
        coll.drop();
        assert.writeOK(coll.insert({_id: 3, a: [{b: 5}]}));
        assert.throws(function() {
            coll.findAndModify({
                query: {_id: 3},
                update: {$set: {"a.$[i].b": 6}},
                arrayFilters: [{"i.b": 5, $isolated: 1}]
            });
        });
    }

    //
    // $isolated in geoNear.
    //

    assert.writeOK(coll.insert({geo: {type: "Point", coordinates: [0, 0]}, a: 5}));
    assert.commandWorked(coll.ensureIndex({geo: "2dsphere"}));
    assert.commandFailed(db.runCommand({
        geoNear: coll.getName(),
        near: {type: "Point", coordinates: [0, 0]},
        spherical: true,
        query: {$isolated: 1}
    }));

    //
    // $isolated in mapReduce.
    //

    assert.writeOK(coll.insert({a: 5}));
    assert.throws(function() {
        coll.mapReduce(
            function() {
                emit(this.a, 1);
            },
            function(key, values) {
                return Array.sum(values);
            },
            {out: {inline: 1}, query: {$isolated: 1}});
    });

    //
    // $isolated in remove.
    //

    coll.drop();
    assert.writeOK(coll.insert({_id: 0, a: 5}));
    let writeRes = coll.remove({_id: 0, $isolated: 1});
    assert.writeOK(writeRes);
    assert.eq(1, writeRes.nRemoved);

    //
    // $isolated in update.
    //

    // $isolated is allowed in the filter of an update command.
    assert.writeOK(coll.insert({_id: 0, a: 5}));
    assert.writeOK(coll.update({_id: 0, $isolated: 1}, {$set: {b: 6}}));
    assert.eq({_id: 0, a: 5, b: 6}, coll.findOne({_id: 0}));

    // $isolated is not allowed in a $pull filter.
    coll.drop();
    assert.writeOK(coll.insert({_id: 0, a: [{b: 5}]}));
    assert.writeErrorWithCode(coll.update({_id: 0}, {$pull: {a: {$isolated: 1}}}),
                              ErrorCodes.QueryFeatureNotAllowed);

    // $isolated is not allowed in arrayFilters.
    if (db.getMongo().writeMode() === "commands") {
        coll.drop();
        assert.writeOK(coll.insert({_id: 0, a: [{b: 5}]}));
        assert.writeErrorWithCode(
            coll.update(
                {_id: 0}, {$set: {"a.$[i].b": 6}}, {arrayFilters: [{"i.b": 5, $isolated: 1}]}),
            ErrorCodes.QueryFeatureNotAllowed);
    }

    //
    // $isolated in a view.
    //

    assert.commandFailed(db.createView("invalid", coll.getName(), [{$match: {$isolated: 1}}]));
    assert.commandFailed(
        db.createView("invalid", coll.getName(), [{$match: {a: {$gt: 5}, $isolated: 1}}]));

    //
    // $isolated in a partial index filter.
    //

    assert.commandFailed(coll.createIndex({a: 1}, {partialFilterExpression: {$isolated: 1}}));
    assert.commandFailed(
        coll.createIndex({a: 1}, {partialFilterExpression: {a: {$lt: 5}, $isolated: 1}}));

})();