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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
// @tags: [
// requires_non_retryable_commands,
// requires_fastcount,
//
// # applyOps uses the oplog that require replication support
// requires_replication,
// ]
(function() {
"use strict";
load("jstests/libs/get_index_helpers.js");
var t = db.apply_ops1;
t.drop();
//
// Input validation tests
//
// Empty array of operations.
assert.commandWorked(db.adminCommand({applyOps: []}),
'applyOps should not fail on empty array of operations');
// Non-array type for operations.
assert.commandFailed(db.adminCommand({applyOps: "not an array"}),
'applyOps should fail on non-array type for operations');
// Missing 'op' field in an operation.
assert.commandFailed(db.adminCommand({applyOps: [{ns: t.getFullName()}]}),
'applyOps should fail on operation without "op" field');
// Non-string 'op' field in an operation.
assert.commandFailed(db.adminCommand({applyOps: [{op: 12345, ns: t.getFullName()}]}),
'applyOps should fail on operation with non-string "op" field');
// Empty 'op' field value in an operation.
assert.commandFailed(db.adminCommand({applyOps: [{op: '', ns: t.getFullName()}]}),
'applyOps should fail on operation with empty "op" field value');
// Missing 'ns' field in an operation.
assert.commandFailed(db.adminCommand({applyOps: [{op: 'c'}]}),
'applyOps should fail on operation without "ns" field');
// Non-string 'ns' field in an operation.
assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: 12345}]}),
'applyOps should fail on operation with non-string "ns" field');
// Empty 'ns' field value in an operation of type 'n' (noop).
assert.commandWorked(db.adminCommand({applyOps: [{op: 'n', ns: ''}]}),
'applyOps should work on no op operation with empty "ns" field value');
// Missing dbname in 'ns' field.
assert.commandFailed(db.adminCommand({applyOps: [{op: 'd', ns: t.getName(), o: {_id: 1}}]}));
// Missing 'o' field value in an operation of type 'c' (command).
assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: t.getFullName()}]}),
'applyOps should fail on command operation without "o" field');
// Non-object 'o' field value in an operation of type 'c' (command).
assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: t.getFullName(), o: 'bar'}]}),
'applyOps should fail on command operation with non-object "o" field');
// Empty object 'o' field value in an operation of type 'c' (command).
assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: t.getFullName(), o: {}}]}),
'applyOps should fail on command operation with empty object "o" field');
// Unknown key in 'o' field value in an operation of type 'c' (command).
assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: t.getFullName(), o: {a: 1}}]}),
'applyOps should fail on command operation on unknown key in "o" field');
// Empty 'ns' field value in operation type other than 'n'.
assert.commandFailed(
db.adminCommand({applyOps: [{op: 'c', ns: ''}]}),
'applyOps should fail on non-"n" operation type with empty "ns" field value');
// Excessively nested applyOps commands gracefully fail.
assert.commandFailed(db.adminCommand({
"applyOps": [{
"ts": {"$timestamp": {"t": 1, "i": 100}},
"h": 0,
"v": 2,
"op": "c",
"ns": "test.$cmd",
"o": {
"applyOps": [{
"ts": {"$timestamp": {"t": 1, "i": 100}},
"h": 0,
"v": 2,
"op": "c",
"ns": "test.$cmd",
"o": {
"applyOps": [{
"ts": {"$timestamp": {"t": 1, "i": 100}},
"h": 0,
"v": 2,
"op": "c",
"ns": "test.$cmd",
"o": {
"applyOps": [{
"ts": {"$timestamp": {"t": 1, "i": 100}},
"h": 0,
"v": 2,
"op": "c",
"ns": "test.$cmd",
"o": {
"applyOps": [{
"ts": {"$timestamp": {"t": 1, "i": 100}},
"h": 0,
"v": 2,
"op": "c",
"ns": "test.$cmd",
"o": {
"applyOps": [{
"ts": {"$timestamp": {"t": 1, "i": 100}},
"h": 0,
"v": 2,
"op": "c",
"ns": "test.$cmd",
"o": {
"applyOps": [{
"ts":
{"$timestamp": {"t": 1, "i": 100}},
"h": 0,
"v": 2,
"op": "c",
"ns": "test.$cmd",
"o": {
"applyOps": [{
"ts": {
"$timestamp":
{"t": 1, "i": 100}
},
"h": 0,
"v": 2,
"op": "c",
"ns": "test.$cmd",
"o": {
"applyOps": [{
"ts": {
"$timestamp": {
"t": 1,
"i": 100
}
},
"h": 0,
"v": 2,
"op": "c",
"ns": "test.$cmd",
"o": {
"applyOps": [{
"ts": {
"$timestamp":
{
"t":
1,
"i":
100
}
},
"h": 0,
"v": 2,
"op": "c",
"ns":
"test.$cmd",
"o": {
"applyOps": [
{
"ts": {
"$timestamp": {
"t":
1,
"i":
100
}
},
"h":
0,
"v":
2,
"op":
"c",
"ns":
"test.$cmd",
"o": {
"applyOps":
[
]
}
}
]
}
}]
}
}]
}
}]
}
}]
}
}]
}
}]
}
}]
}
}]
}
}]
}
}]
}),
"Excessively nested applyOps should be rejected");
// Valid 'ns' field value in unknown operation type 'x'.
assert.commandFailed(
db.adminCommand({applyOps: [{op: 'x', ns: t.getFullName()}]}),
'applyOps should fail on unknown operation type "x" with valid "ns" value');
assert.eq(0, t.find().count(), "Non-zero amount of documents in collection to start");
/**
* Test function for running CRUD operations on non-existent namespaces using various
* combinations of invalid namespaces (collection/database), allowAtomic and alwaysUpsert,
* and nesting.
*
* Leave 'expectedErrorCode' undefined if this command is expected to run successfully.
*/
function testCrudOperationOnNonExistentNamespace(optype, o, o2, expectedErrorCode) {
expectedErrorCode = expectedErrorCode || ErrorCodes.OK;
const t2 = db.getSiblingDB('apply_ops1_no_such_db').getCollection('t');
[t, t2].forEach(coll => {
const op = {op: optype, ns: coll.getFullName(), o: o, o2: o2};
[false, true].forEach(nested => {
const opToRun =
nested ? {op: 'c', ns: 'test.$cmd', o: {applyOps: [op]}, o2: {}} : op;
[false, true].forEach(allowAtomic => {
[false, true].forEach(alwaysUpsert => {
const cmd = {
applyOps: [opToRun],
allowAtomic: allowAtomic,
alwaysUpsert: alwaysUpsert
};
jsTestLog('Testing applyOps on non-existent namespace: ' + tojson(cmd));
if (expectedErrorCode === ErrorCodes.OK) {
assert.commandWorked(db.adminCommand(cmd));
} else {
assert.commandFailedWithCode(db.adminCommand(cmd), expectedErrorCode);
}
});
});
});
});
}
// Insert and update operations on non-existent collections/databases should return
// NamespaceNotFound.
testCrudOperationOnNonExistentNamespace('i', {_id: 0}, {}, ErrorCodes.NamespaceNotFound);
testCrudOperationOnNonExistentNamespace('u', {x: 0}, {_id: 0}, ErrorCodes.NamespaceNotFound);
// Delete operations on non-existent collections/databases should return OK for idempotency
// reasons.
testCrudOperationOnNonExistentNamespace('d', {_id: 0}, {});
assert.commandWorked(db.createCollection(t.getName()));
var a = assert.commandWorked(
db.adminCommand({applyOps: [{"op": "i", "ns": t.getFullName(), "o": {_id: 5, x: 17}}]}));
assert.eq(1, t.find().count(), "Valid insert failed");
assert.eq(true, a.results[0], "Bad result value for valid insert");
a = assert.commandWorked(
db.adminCommand({applyOps: [{"op": "i", "ns": t.getFullName(), "o": {_id: 5, x: 17}}]}));
assert.eq(1, t.find().count(), "Duplicate insert failed");
assert.eq(true, a.results[0], "Bad result value for duplicate insert");
var o = {_id: 5, x: 17};
assert.eq(o, t.findOne(), "Mismatching document inserted.");
// 'o' field is an empty array.
assert.commandFailed(db.adminCommand({applyOps: [{op: 'i', ns: t.getFullName(), o: []}]}),
'applyOps should fail on insert of object with empty array element');
var res = assert.commandWorked(db.runCommand({
applyOps: [
{op: "u", ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 18}}},
{op: "u", ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 19}}}
]
}));
o.x++;
o.x++;
assert.eq(1, t.find().count(), "Updates increased number of documents");
assert.eq(o, t.findOne(), "Document doesn't match expected");
assert.eq(true, res.results[0], "Bad result value for valid update");
assert.eq(true, res.results[1], "Bad result value for valid update");
// preCondition fully matches
res = db.runCommand({
applyOps: [
{op: "u", ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 20}}},
{op: "u", ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 21}}}
],
preCondition: [{ns: t.getFullName(), q: {_id: 5}, res: {x: 19}}]
});
// The use of preCondition requires applyOps to run atomically. Therefore, it is incompatible
// with {allowAtomic: false}.
assert.commandFailedWithCode(
db.runCommand({
applyOps: [{op: 'u', ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 22}}}],
preCondition: [{ns: t.getFullName(), q: {_id: 5}, res: {x: 21}}],
allowAtomic: false,
}),
ErrorCodes.InvalidOptions,
'applyOps should fail when preCondition is present and atomicAllowed is false.');
// The use of preCondition is also incompatible with operations that include commands.
assert.commandFailedWithCode(
db.runCommand({
applyOps: [{op: 'c', ns: t.getCollection('$cmd').getFullName(), o: {applyOps: []}}],
preCondition: [{ns: t.getFullName(), q: {_id: 5}, res: {x: 21}}],
}),
ErrorCodes.InvalidOptions,
'applyOps should fail when preCondition is present and operations includes commands.');
o.x++;
o.x++;
assert.eq(1, t.find().count(), "Updates increased number of documents");
assert.eq(o, t.findOne(), "Document doesn't match expected");
assert.eq(true, res.results[0], "Bad result value for valid update");
assert.eq(true, res.results[1], "Bad result value for valid update");
// preCondition doesn't match ns
res = db.runCommand({
applyOps: [
{op: "u", ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 22}}},
{op: "u", ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 23}}}
],
preCondition: [{ns: "foo.otherName", q: {_id: 5}, res: {x: 21}}]
});
assert.eq(o, t.findOne(), "preCondition didn't match, but ops were still applied");
// preCondition doesn't match query
res = db.runCommand({
applyOps: [
{op: "u", ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 22}}},
{op: "u", ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 23}}}
],
preCondition: [{ns: t.getFullName(), q: {_id: 5}, res: {x: 19}}]
});
assert.eq(o, t.findOne(), "preCondition didn't match, but ops were still applied");
res = db.runCommand({
applyOps: [
{op: "u", ns: t.getFullName(), o2: {_id: 5}, o: {$set: {x: 22}}},
{op: "u", ns: t.getFullName(), o2: {_id: 6}, o: {$set: {x: 23}}}
]
});
assert.eq(true, res.results[0], "Valid update failed");
assert.eq(true, res.results[1], "Valid update failed");
// Ops with transaction numbers are valid.
var lsid = {id: UUID()};
res = db.runCommand({
applyOps: [
{
op: "i",
ns: t.getFullName(),
o: {_id: 7, x: 24},
lsid: lsid,
txnNumber: NumberLong(1),
stmdId: 0
},
{
op: "u",
ns: t.getFullName(),
o2: {_id: 8},
o: {$set: {x: 25}},
lsid: lsid,
txnNumber: NumberLong(1),
stmdId: 1
},
{
op: "d",
ns: t.getFullName(),
o: {_id: 7},
lsid: lsid,
txnNumber: NumberLong(2),
stmdId: 0
},
]
});
assert.eq(true, res.results[0], "Valid insert with transaction number failed");
assert.eq(true, res.results[1], "Valid update with transaction number failed");
assert.eq(true, res.results[2], "Valid delete with transaction number failed");
// When applying a "u" (update) op, we default to 'UpdateNode' update semantics, and $set
// operations add new fields in lexicographic order.
res = assert.commandWorked(db.adminCommand({
applyOps: [
{"op": "i", "ns": t.getFullName(), "o": {_id: 6}},
{"op": "u", "ns": t.getFullName(), "o2": {_id: 6}, "o": {$set: {z: 1, a: 2}}}
]
}));
assert.eq(t.findOne({_id: 6}), {_id: 6, a: 2, z: 1}); // Note: 'a' and 'z' have been sorted.
// 'ModifierInterface' semantics are not supported, so an update with {$v: 0} should fail.
res = assert.commandFailed(db.adminCommand({
applyOps: [
{"op": "i", "ns": t.getFullName(), "o": {_id: 7}},
{
"op": "u",
"ns": t.getFullName(),
"o2": {_id: 7},
"o": {$v: NumberLong(0), $set: {z: 1, a: 2}}
}
]
}));
assert.eq(res.code, 40682);
// When we explicitly specify {$v: 1}, we should get 'UpdateNode' update semantics, and $set
// operations get performed in lexicographic order.
res = assert.commandWorked(db.adminCommand({
applyOps: [
{"op": "i", "ns": t.getFullName(), "o": {_id: 8}},
{
"op": "u",
"ns": t.getFullName(),
"o2": {_id: 8},
"o": {$v: NumberLong(1), $set: {z: 1, a: 2}}
}
]
}));
assert.eq(t.findOne({_id: 8}), {_id: 8, a: 2, z: 1}); // Note: 'a' and 'z' have been sorted.
})();
|