summaryrefslogtreecommitdiff
path: root/jstests/sharding/upsert_sharded.js
blob: 84065b120d356e3e8215479ac340ab79a8ff320e (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// Upsert behavior tests for sharding. If updateOneWithoutShardKey feature flag is enabled, upsert
// operations with queries that do not match on the entire shard key are successful. NOTE: Generic
// upsert behavior tests belong in the core suite
//
(function() {
'use strict';

load("jstests/sharding/updateOne_without_shard_key/libs/write_without_shard_key_test_util.js");

const st = new ShardingTest({shards: 2, mongos: 1});

const mongos = st.s0;
const admin = mongos.getDB("admin");
const coll = mongos.getCollection("foo.bar");

assert(admin.runCommand({enableSharding: coll.getDB() + ""}).ok);
st.ensurePrimaryShard(coll.getDB().getName(), st.shard1.shardName);

const upsertSuppliedResult = function(upsertColl, query, newDoc) {
    assert.commandWorked(upsertColl.remove({}));
    return coll.runCommand({
        update: coll.getName(),
        updates: [{
            q: query,
            u: [{$addFields: {unused: true}}],
            c: {new: newDoc},
            upsert: true,
            upsertSupplied: true
        }]
    });
};

const upsertedResult = function(upsertColl, query, expr) {
    assert.commandWorked(upsertColl.remove({}));
    return upsertColl.update(query, expr, {upsert: true});
};

const upsertedField = function(upsertColl, query, expr, fieldName) {
    assert.commandWorked(upsertedResult(upsertColl, query, expr));
    return upsertColl.findOne()[fieldName];
};

const upsertedXVal = function(upsertColl, query, expr) {
    return upsertedField(upsertColl, query, expr, "x");
};

//
// Tests for non-nested shard key.
//
st.ensurePrimaryShard(coll.getDB() + "", st.shard0.shardName);
assert.commandWorked(admin.runCommand({shardCollection: coll + "", key: {x: 1}}));
assert.commandWorked(admin.runCommand({split: coll + "", middle: {x: 0}}));
assert.commandWorked(admin.runCommand(
    {moveChunk: coll + "", find: {x: 0}, to: st.shard1.shardName, _waitForDelete: true}));

st.printShardingStatus();

// Upserted replacement update can result in no shard key.
assert.commandWorked(upsertedResult(coll, {x: -1}, {_id: 1}));
assert.docEq({_id: 1}, coll.findOne({}));

// Upserted with supplied document can result in no shard key.
assert.commandWorked(upsertSuppliedResult(coll, {x: -1}, {_id: 1}));
assert.docEq({_id: 1}, coll.findOne({}));

// Upserted op style update will propagate shard key by default.
assert.commandWorked(upsertedResult(coll, {x: -1}, {$set: {_id: 1}}));
assert.docEq({_id: 1, x: -1}, coll.findOne({}));

// Upserted op style update can unset propagated shard key.
assert.commandWorked(upsertedResult(coll, {x: -1}, {$set: {_id: 1}, $unset: {x: 1}}));
assert.docEq({_id: 1}, coll.findOne({}));

// Updates with upsert must contain shard key in query when $op style
assert.eq(1, upsertedXVal(coll, {x: 1}, {$set: {a: 1}}));
assert.eq(1, upsertedXVal(coll, {x: {$eq: 1}}, {$set: {a: 1}}));
assert.eq(1, upsertedXVal(coll, {x: {$all: [1]}}, {$set: {a: 1}}));
assert.eq(1, upsertedXVal(coll, {x: {$in: [1]}}, {$set: {a: 1}}));
assert.eq(1, upsertedXVal(coll, {$and: [{x: {$eq: 1}}]}, {$set: {a: 1}}));
assert.eq(1, upsertedXVal(coll, {$or: [{x: {$eq: 1}}]}, {$set: {a: 1}}));

if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(coll.getDB())) {
    assert.eq(1, upsertedXVal(coll, {}, {$set: {a: 1, x: 1}}));
    assert.eq(5, upsertedXVal(coll, {x: {$gt: 10}}, {$set: {a: 1, x: 5}}));
    assert.eq("regexValue",
              upsertedXVal(coll, {x: {$eq: /abc*/}}, {$set: {a: 1, x: "regexValue"}}));
    assert.eq(/abc/, upsertedXVal(coll, {x: {$eq: /abc/}}, {$set: {a: 1, x: /abc/}}));
    assert.eq({$gt: 5}, upsertedXVal(coll, {x: {$eq: {$gt: 5}}}, {$set: {a: 1}}));
    assert.eq({x: 1}, upsertedXVal(coll, {"x.x": 1}, {$set: {a: 1}}));
    assert.eq({x: 1}, upsertedXVal(coll, {"x.x": {$eq: 1}}, {$set: {a: 1}}));
} else {
    // Missing shard key in query.
    assert.commandFailedWithCode(upsertedResult(coll, {}, {$set: {a: 1, x: 1}}),
                                 ErrorCodes.ShardKeyNotFound);

    // Missing equality match on shard key in query.
    assert.commandFailedWithCode(upsertedResult(coll, {x: {$gt: 10}}, {$set: {a: 1, x: 5}}),
                                 ErrorCodes.ShardKeyNotFound);

    // Regex shard key value in query is ambigious and cannot be extracted for an equality match.
    assert.commandFailedWithCode(
        upsertedResult(coll, {x: {$eq: /abc*/}}, {$set: {a: 1, x: "regexValue"}}),
        ErrorCodes.ShardKeyNotFound);
    assert.commandFailedWithCode(upsertedResult(coll, {x: {$eq: /abc/}}, {$set: {a: 1, x: /abc/}}),
                                 ErrorCodes.ShardKeyNotFound);

    // Shard key in query is not extractable.
    assert.commandFailedWithCode(upsertedResult(coll, {x: {$eq: {$gt: 5}}}, {$set: {a: 1}}),
                                 ErrorCodes.ShardKeyNotFound);

    // Nested field extraction always fails with non-nested key - like _id, we require setting the
    // elements directly
    assert.commandFailedWithCode(upsertedResult(coll, {"x.x": 1}, {$set: {a: 1}}),
                                 ErrorCodes.ShardKeyNotFound);
    assert.commandFailedWithCode(upsertedResult(coll, {"x.x": {$eq: 1}}, {$set: {a: 1}}),
                                 ErrorCodes.ShardKeyNotFound);
}

// Shard key in query not extractable.
assert.commandFailedWithCode(upsertedResult(coll, {x: undefined}, {$set: {a: 1}}),
                             ErrorCodes.BadValue);
assert.commandFailedWithCode(upsertedResult(coll, {x: [1, 2]}, {$set: {a: 1}}),
                             ErrorCodes.ShardKeyNotFound);

coll.drop();

//
// Tests for nested shard key.
//
st.ensurePrimaryShard(coll.getDB() + "", st.shard0.shardName);
assert.commandWorked(admin.runCommand({shardCollection: coll + "", key: {'x.x': 1}}));
assert.commandWorked(admin.runCommand({split: coll + "", middle: {'x.x': 0}}));
assert.commandWorked(admin.runCommand(
    {moveChunk: coll + "", find: {'x.x': 0}, to: st.shard1.shardName, _waitForDelete: true}));

st.printShardingStatus();

// Upserted replacement update can result in no shard key with nested shard key.
assert.commandWorked(upsertedResult(coll, {"x.x": -1}, {_id: 1}));
assert.docEq({_id: 1}, coll.findOne({}));

// Upserted with supplied document can result in no shard key with nested shard key.
assert.commandWorked(upsertSuppliedResult(coll, {"x.x": -1}, {_id: 1}));
assert.docEq({_id: 1}, coll.findOne({}));

// Upserted op style update will propagate shard key by default with nested shard key.
assert.commandWorked(upsertedResult(coll, {"x.x": -1}, {$set: {_id: 1}}));
assert.docEq({_id: 1, x: {x: -1}}, coll.findOne({}));

// Upserted op style update can unset propagated shard key fields with nested shard key.
assert.commandWorked(upsertedResult(coll, {"x.x": -1}, {$set: {_id: 1}, $unset: {"x.x": 1}}));
assert.docEq({_id: 1, x: {}}, coll.findOne({}));

assert.commandWorked(upsertedResult(coll, {"x.x": -1}, {$set: {_id: 1}, $unset: {"x": 1}}));
assert.docEq({_id: 1}, coll.findOne({}));

// Nested field extraction with nested shard key
assert.docEq({x: 1}, upsertedXVal(coll, {"x.x": 1}, {$set: {a: 1}}));
assert.docEq({x: 1}, upsertedXVal(coll, {"x.x": {$eq: 1}}, {$set: {a: 1}}));
assert.docEq({x: 1}, upsertedXVal(coll, {"x.x": {$all: [1]}}, {$set: {a: 1}}));
assert.docEq({x: 1}, upsertedXVal(coll, {$and: [{"x.x": {$eq: 1}}]}, {$set: {a: 1}}));
assert.docEq({x: 1}, upsertedXVal(coll, {$or: [{"x.x": {$eq: 1}}]}, {$set: {a: 1}}));

// Can specify siblings of nested shard keys
assert.docEq({x: 1, y: 1}, upsertedXVal(coll, {"x.x": 1, "x.y": 1}, {$set: {a: 1}}));
assert.docEq({x: 1, y: {z: 1}}, upsertedXVal(coll, {"x.x": 1, "x.y.z": 1}, {$set: {a: 1}}));

// No arrays at any level for targeting.
assert.commandFailedWithCode(upsertedResult(coll, {"x.x": []}, {$set: {a: 1}}),
                             ErrorCodes.ShardKeyNotFound);
assert.commandFailedWithCode(upsertedResult(coll, {x: {x: []}}, {$set: {a: 1}}),
                             ErrorCodes.ShardKeyNotFound);
assert.commandFailedWithCode(upsertedResult(coll, {x: [{x: 1}]}, {$set: {a: 1}}),
                             ErrorCodes.ShardKeyNotFound);

// No arrays at any level for document insertion for replacement, supplied, and op updates.
assert.commandFailedWithCode(upsertedResult(coll, {"x.x": -1}, {$set: {x: {x: []}}}),
                             ErrorCodes.NotSingleValueField);
assert.commandFailedWithCode(upsertedResult(coll, {"x.x": -1}, {$set: {x: [{x: 1}]}}),
                             ErrorCodes.NotSingleValueField);

assert.commandFailedWithCode(upsertSuppliedResult(coll, {"x.x": -1}, {x: {x: []}}),
                             ErrorCodes.NotSingleValueField);
assert.commandFailedWithCode(upsertSuppliedResult(coll, {"x.x": -1}, {x: [{x: 1}]}),
                             ErrorCodes.NotSingleValueField);

assert.commandFailedWithCode(upsertedResult(coll, {"x.x": -1}, {x: {x: []}}),
                             ErrorCodes.NotSingleValueField);
assert.commandFailedWithCode(upsertedResult(coll, {"x.x": -1}, {x: [{x: 1}]}),
                             ErrorCodes.NotSingleValueField);

if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(coll.getDB())) {
    assert.eq({x: {x: 1}}, upsertedXVal(coll, {"x.x.x": {$eq: 1}}, {$set: {a: 1}}));
} else {
    // Can't set sub-fields of nested key
    assert.commandFailedWithCode(upsertedResult(coll, {"x.x.x": {$eq: 1}}, {$set: {a: 1}}),
                                 ErrorCodes.ShardKeyNotFound);
}

coll.drop();

//
// Tests for nested _id shard key.
//
st.ensurePrimaryShard(coll.getDB() + "", st.shard0.shardName);
assert.commandWorked(admin.runCommand({shardCollection: coll + "", key: {'_id.x': 1}}));
assert.commandWorked(admin.runCommand({split: coll + "", middle: {'_id.x': 0}}));
assert.commandWorked(admin.runCommand(
    {moveChunk: coll + "", find: {'_id.x': 0}, to: st.shard1.shardName, _waitForDelete: true}));

st.printShardingStatus();

// No upsert type can result in a missing shard key for nested _id key.
assert.commandWorked(upsertedResult(coll, {_id: {x: -1}}, {}));
assert.docEq({_id: {x: -1}}, coll.findOne({}));

assert.commandWorked(upsertSuppliedResult(coll, {_id: {x: -1}}, {}));
assert.docEq({_id: {x: -1}}, coll.findOne({}));

assert.commandWorked(upsertedResult(coll, {_id: {x: -1}}, {$set: {y: 1}}));
assert.docEq({_id: {x: -1}, y: 1}, coll.findOne({}));

assert.commandFailedWithCode(
    upsertedResult(coll, {_id: {x: -1}}, {$set: {y: 1}, $unset: {"_id.x": 1}}),
    ErrorCodes.ImmutableField);

// All update types can re-state shard key for nested _id key.
assert.commandWorked(upsertedResult(coll, {_id: {x: -1}}, {_id: {x: -1}, y: 1}));
assert.docEq({_id: {x: -1}, y: 1}, coll.findOne({}));

assert.commandWorked(upsertSuppliedResult(coll, {_id: {x: -1}}, {_id: {x: -1}, y: 1}));
assert.docEq({_id: {x: -1}, y: 1}, coll.findOne({}));

assert.commandWorked(upsertedResult(coll, {_id: {x: -1}}, {$set: {_id: {x: -1}, y: 1}}));
assert.docEq({_id: {x: -1}, y: 1}, coll.findOne({}));

assert.commandWorked(upsertedResult(coll, {_id: {x: -1}}, {$set: {"_id.x": -1, y: 1}}));
assert.docEq({_id: {x: -1}, y: 1}, coll.findOne({}));

// No upsert type can modify shard key for nested _id key.
assert.commandFailedWithCode(upsertedResult(coll, {_id: {x: -1}}, {_id: {x: -2}}),
                             ErrorCodes.ImmutableField);

assert.commandFailedWithCode(upsertSuppliedResult(coll, {_id: {x: -1}}, {_id: {x: -2}}),
                             ErrorCodes.ImmutableField);

assert.commandFailedWithCode(upsertedResult(coll, {_id: {x: -1}}, {$set: {_id: {x: -2}}}),
                             ErrorCodes.ImmutableField);

// No upsert type can add new _id subfield for nested _id key.
assert.commandFailedWithCode(upsertedResult(coll, {_id: {x: -1}}, {_id: {x: -1, y: -1}}),
                             ErrorCodes.ImmutableField);

assert.commandFailedWithCode(upsertSuppliedResult(coll, {_id: {x: -1}}, {_id: {x: -1, y: -1}}),
                             ErrorCodes.ImmutableField);

assert.commandFailedWithCode(
    upsertedResult(coll, {_id: {x: -1}}, {$set: {"_id.x": -1, "_id.y": -1}}),
    ErrorCodes.ImmutableField);

// No upsert type can remove non-shardkey _id subfield for nested _id key.
assert.commandFailedWithCode(upsertedResult(coll, {_id: {x: -1, y: -1}}, {_id: {x: -1}}),
                             ErrorCodes.ImmutableField);

assert.commandFailedWithCode(upsertSuppliedResult(coll, {_id: {x: -1, y: -1}}, {_id: {x: -1}}),
                             ErrorCodes.ImmutableField);

assert.commandFailedWithCode(upsertedResult(coll, {_id: {x: -1, y: -1}}, {$unset: {"_id.y": 1}}),
                             ErrorCodes.ImmutableField);

if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(coll.getDB())) {
    // Incorrect format of _id or shard key errors.
    assert.commandFailedWithCode(
        upsertedResult(coll, {_id: {x: [1]}}, {}),
        ErrorCodes
            .ShardKeyNotFound);  // Shard key cannot contain array values or array descendants.
    assert.commandFailedWithCode(upsertedResult(coll, {"_id.x": [1]}, {}),
                                 ErrorCodes.NotExactValueField);
    assert.commandFailedWithCode(upsertedResult(coll, {_id: [{x: 1}]}, {}),
                                 ErrorCodes.NotSingleValueField);

    assert.commandFailedWithCode(upsertSuppliedResult(coll, {_id: {x: [1]}}, {}),
                                 ErrorCodes.NotSingleValueField);
    assert.commandFailedWithCode(
        upsertSuppliedResult(coll, {"_id.x": [1]}, {}),
        ErrorCodes
            .ShardKeyNotFound);  // Shard key cannot contain array values or array descendants.
    assert.commandFailedWithCode(upsertSuppliedResult(coll, {_id: [{x: 1}]}, {}),
                                 ErrorCodes.NotSingleValueField);

    assert.commandFailedWithCode(upsertedResult(coll, {_id: {x: [1]}}, {$set: {y: 1}}),
                                 ErrorCodes.NotSingleValueField);
    assert.commandFailedWithCode(
        upsertedResult(coll, {"_id.x": [1]}, {$set: {y: 1}}),
        ErrorCodes
            .ShardKeyNotFound);  // Shard key cannot contain array values or array descendants.
    assert.commandFailedWithCode(
        upsertedResult(coll, {_id: [{x: 1}]}, {$set: {y: 1}}),
        ErrorCodes
            .ShardKeyNotFound);  // Shard key cannot contain array values or array descendants.
} else {
    // No upsert type can set array element for nested _id key.
    assert.commandFailedWithCode(upsertedResult(coll, {_id: {x: [1]}}, {}),
                                 ErrorCodes.ShardKeyNotFound);
    assert.commandFailedWithCode(upsertedResult(coll, {"_id.x": [1]}, {}),
                                 ErrorCodes.ShardKeyNotFound);
    assert.commandFailedWithCode(upsertedResult(coll, {_id: [{x: 1}]}, {}),
                                 ErrorCodes.ShardKeyNotFound);

    assert.commandFailedWithCode(upsertSuppliedResult(coll, {_id: {x: [1]}}, {}),
                                 ErrorCodes.ShardKeyNotFound);
    assert.commandFailedWithCode(upsertSuppliedResult(coll, {"_id.x": [1]}, {}),
                                 ErrorCodes.ShardKeyNotFound);
    assert.commandFailedWithCode(upsertSuppliedResult(coll, {_id: [{x: 1}]}, {}),
                                 ErrorCodes.ShardKeyNotFound);

    assert.commandFailedWithCode(upsertedResult(coll, {_id: {x: [1]}}, {$set: {y: 1}}),
                                 ErrorCodes.ShardKeyNotFound);
    assert.commandFailedWithCode(upsertedResult(coll, {"_id.x": [1]}, {$set: {y: 1}}),
                                 ErrorCodes.ShardKeyNotFound);
    assert.commandFailedWithCode(upsertedResult(coll, {_id: [{x: 1}]}, {$set: {y: 1}}),
                                 ErrorCodes.ShardKeyNotFound);
}

// Replacement and op-style {$set _id} fail when using dotted-path query on nested _id key.
// TODO SERVER-44615: these tests should succeed when SERVER-44615 is complete.
assert.commandFailedWithCode(upsertedResult(coll, {"_id.x": -1}, {_id: {x: -1}}),
                             ErrorCodes.NotExactValueField);

assert.commandWorked(upsertSuppliedResult(coll, {"_id.x": -1}, {_id: {x: -1}}));
assert.docEq({_id: {x: -1}}, coll.findOne({}));

assert.commandFailedWithCode(upsertedResult(coll, {"_id.x": -1}, {$set: {_id: {x: -1}}}),
                             ErrorCodes.ImmutableField);

assert.commandWorked(upsertedResult(coll, {"_id.x": -1}, {$set: {"_id.x": -1}}));
assert.docEq({_id: {x: -1}}, coll.findOne({}));

st.stop();
})();