summaryrefslogtreecommitdiff
path: root/jstests/core/update_arrayFilters.js
blob: 4271eb8cddbc01de65d14e0ffa9782824e345d1e (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Tests for the arrayFilters option to update and findAndModify.
(function() {
    "use strict";

    let coll = db.update_arrayFilters;
    coll.drop();

    //
    // Tests for update.
    //

    if (db.getMongo().writeMode() !== "commands") {
        assert.throws(function() {
            coll.update({_id: 0}, {$set: {a: 5}}, {arrayFilters: [{i: 0}]});
        });
    } else {
        // Non-array arrayFilters fails to parse.
        assert.writeError(coll.update({_id: 0}, {$set: {a: 5}}, {arrayFilters: {i: 0}}),
                          ErrorCodes.TypeMismatch);

        // Non-object array filter fails to parse.
        assert.writeError(coll.update({_id: 0}, {$set: {a: 5}}, {arrayFilters: ["bad"]}),
                          ErrorCodes.TypeMismatch);

        // Bad array filter fails to parse.
        let res = coll.update({_id: 0}, {$set: {a: 5}}, {arrayFilters: [{i: 0, j: 0}]});
        assert.writeError(res, ErrorCodes.FailedToParse);
        assert.neq(-1,
                   res.getWriteError().errmsg.indexOf(
                       "Each array filter must use a single top-level field name"),
                   "update failed for a reason other than failing to parse array filters");

        // Multiple array filters with the same id fails to parse.
        res = coll.update({_id: 0}, {$set: {a: 5}}, {arrayFilters: [{i: 0}, {j: 0}, {i: 1}]});
        assert.writeError(res, ErrorCodes.FailedToParse);
        assert.neq(
            -1,
            res.getWriteError().errmsg.indexOf(
                "Found multiple array filters with the same top-level field name"),
            "update failed for a reason other than multiple array filters with the same top-level field name");

        // Good value for arrayFilters succeeds.
        assert.writeOK(coll.update({_id: 0}, {$set: {a: 5}}, {arrayFilters: [{i: 0}, {j: 0}]}));
    }

    //
    // Tests for findAndModify.
    //

    // Non-array arrayFilters fails to parse.
    assert.throws(function() {
        coll.findAndModify({query: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: {i: 0}});
    });

    // Non-object array filter fails to parse.
    assert.throws(function() {
        coll.findAndModify({query: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: ["bad"]});
    });

    // arrayFilters option not allowed with remove=true.
    assert.throws(function() {
        coll.findAndModify({query: {_id: 0}, remove: true, arrayFilters: [{i: 0}]});
    });

    // Bad array filter fails to parse.
    assert.throws(function() {
        coll.findAndModify({query: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: [{i: 0, j: 0}]});
    });

    // Multiple array filters with the same id fails to parse.
    assert.throws(function() {
        coll.findAndModify(
            {query: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: [{i: 0}, {j: 0}, {i: 1}]});
    });

    // Good value for arrayFilters succeeds.
    assert.eq(null,
              coll.findAndModify(
                  {query: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: [{i: 0}, {j: 0}]}));

    //
    // Tests for the bulk API.
    //

    if (db.getMongo().writeMode() !== "commands") {
        let bulk = coll.initializeUnorderedBulkOp();
        bulk.find({});
        assert.throws(function() {
            bulk.arrayFilters([{i: 0}]);
        });
    } else {
        // update().
        let bulk = coll.initializeUnorderedBulkOp();
        bulk.find({}).arrayFilters("bad").update({$set: {a: 5}});
        assert.throws(function() {
            bulk.execute();
        });
        bulk = coll.initializeUnorderedBulkOp();
        bulk.find({}).arrayFilters([{i: 0}]).update({$set: {a: 5}});
        assert.writeOK(bulk.execute());

        // updateOne().
        bulk = coll.initializeUnorderedBulkOp();
        bulk.find({_id: 0}).arrayFilters("bad").updateOne({$set: {a: 5}});
        assert.throws(function() {
            bulk.execute();
        });
        bulk = coll.initializeUnorderedBulkOp();
        bulk.find({_id: 0}).arrayFilters([{i: 0}]).updateOne({$set: {a: 5}});
        assert.writeOK(bulk.execute());
    }

    //
    // Tests for the CRUD API.
    //

    // findOneAndUpdate().
    assert.throws(function() {
        coll.findOneAndUpdate({_id: 0}, {$set: {a: 5}}, {arrayFilters: "bad"});
    });
    assert.eq(null, coll.findOneAndUpdate({_id: 0}, {$set: {a: 5}}, {arrayFilters: [{i: 0}]}));

    // updateOne().
    if (db.getMongo().writeMode() !== "commands") {
        assert.throws(function() {
            coll.updateOne({_id: 0}, {$set: {a: 5}}, {arrayFilters: [{i: 0}]});
        });
    } else {
        assert.throws(function() {
            coll.updateOne({_id: 0}, {$set: {a: 5}}, {arrayFilters: "bad"});
        });
        let res = coll.updateOne({_id: 0}, {$set: {a: 5}}, {arrayFilters: [{i: 0}]});
        assert.eq(0, res.modifiedCount);
    }

    // updateMany().
    if (db.getMongo().writeMode() !== "commands") {
        assert.throws(function() {
            coll.updateMany({}, {$set: {a: 5}}, {arrayFilters: [{i: 0}]});
        });
    } else {
        assert.throws(function() {
            coll.updateMany({}, {$set: {a: 5}}, {arrayFilters: "bad"});
        });
        let res = coll.updateMany({}, {$set: {a: 5}}, {arrayFilters: [{i: 0}]});
        assert.eq(0, res.modifiedCount);
    }

    // updateOne with bulkWrite().
    if (db.getMongo().writeMode() !== "commands") {
        assert.throws(function() {
            coll.bulkWrite(
                [{updateOne: {filter: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: [{i: 0}]}}]);
        });
    } else {
        assert.throws(function() {
            coll.bulkWrite(
                [{updateOne: {filter: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: "bad"}}]);
        });
        let res = coll.bulkWrite(
            [{updateOne: {filter: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: [{i: 0}]}}]);
        assert.eq(0, res.matchedCount);
    }

    // updateMany with bulkWrite().
    if (db.getMongo().writeMode() !== "commands") {
        assert.throws(function() {
            coll.bulkWrite(
                [{updateMany: {filter: {}, update: {$set: {a: 5}}, arrayFilters: [{i: 0}]}}]);
        });
    } else {
        assert.throws(function() {
            coll.bulkWrite(
                [{updateMany: {filter: {}, update: {$set: {a: 5}}, arrayFilters: "bad"}}]);
        });
        let res = coll.bulkWrite(
            [{updateMany: {filter: {}, update: {$set: {a: 5}}, arrayFilters: [{i: 0}]}}]);
        assert.eq(0, res.matchedCount);
    }

    //
    // Tests for explain().
    //

    // update().
    if (db.getMongo().writeMode() !== "commands") {
        assert.throws(function() {
            coll.explain().update({_id: 0}, {$set: {a: 5}}, {arrayFilters: [{i: 0}]});
        });
    } else {
        assert.throws(function() {
            coll.explain().update({_id: 0}, {$set: {a: 5}}, {arrayFilters: "bad"});
        });
        assert.commandWorked(
            coll.explain().update({_id: 0}, {$set: {a: 5}}, {arrayFilters: [{i: 0}]}));
    }

    // findAndModify().
    assert.throws(function() {
        coll.explain().findAndModify(
            {query: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: "bad"});
    });
    assert.commandWorked(coll.explain().findAndModify(
        {query: {_id: 0}, update: {$set: {a: 5}}, arrayFilters: [{i: 0}]}));
})();