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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
(function() {
"use strict";
var crudAPISpecTests = function crudAPISpecTests() {
"use strict";
// Get the colllection
var coll = db.crud_tests;
// Setup
function createTestExecutor(coll, method, verifyResult) {
return function(args) {
// Drop collection
coll.drop();
// Insert test data
var r = coll.insertMany(args.insert);
assert.eq(args.insert.length, r.insertedIds.length);
// Execute the method with arguments
r = coll[method].apply(coll, args.params);
verifyResult(args.result, r);
// Get all the results
var results = coll.find({}).sort({_id: 1}).toArray();
assert.docEq(args.expected, results);
};
}
function checkResultObject(first, second) {
// Only assert on the "modifiedCount" property when write commands are enabled
if (db.getMongo().writeMode() === 'commands') {
assert.docEq(first, second);
} else {
var overrideModifiedCount = {
modifiedCount: undefined
};
assert.docEq(Object.merge(first, overrideModifiedCount),
Object.merge(second, overrideModifiedCount));
}
}
// Setup executors
var deleteManyExecutor = createTestExecutor(coll, 'deleteMany', checkResultObject);
var deleteOneExecutor = createTestExecutor(coll, 'deleteOne', checkResultObject);
var bulkWriteExecutor = createTestExecutor(coll, 'bulkWrite', checkResultObject);
var findOneAndDeleteExecutor =
createTestExecutor(coll, 'findOneAndDelete', checkResultObject);
var findOneAndReplaceExecutor =
createTestExecutor(coll, 'findOneAndReplace', checkResultObject);
var findOneAndUpdateExecutor =
createTestExecutor(coll, 'findOneAndUpdate', checkResultObject);
var insertManyExecutor = createTestExecutor(coll, 'insertMany', checkResultObject);
var insertOneExecutor = createTestExecutor(coll, 'insertOne', checkResultObject);
var replaceOneExecutor = createTestExecutor(coll, 'replaceOne', checkResultObject);
var updateManyExecutor = createTestExecutor(coll, 'updateMany', checkResultObject);
var updateOneExecutor = createTestExecutor(coll, 'updateOne', checkResultObject);
var countExecutor = createTestExecutor(coll, 'count', assert.eq);
var distinctExecutor = createTestExecutor(coll, 'distinct', assert.eq);
//
// BulkWrite
//
bulkWriteExecutor({
insert: [{_id: 1, c: 1}, {_id: 2, c: 2}, {_id: 3, c: 3}],
params: [[
{insertOne: {document: {_id: 4, a: 1}}},
{updateOne: {filter: {_id: 5, a: 2}, update: {$set: {a: 2}}, upsert: true}},
{updateMany: {filter: {_id: 6, a: 3}, update: {$set: {a: 3}}, upsert: true}},
{deleteOne: {filter: {c: 1}}},
{insertOne: {document: {_id: 7, c: 2}}},
{deleteMany: {filter: {c: 2}}},
{replaceOne: {filter: {c: 3}, replacement: {c: 4}, upsert: true}}
]],
result: {
acknowledged: true,
insertedCount: 2,
matchedCount: 1,
deletedCount: 3,
upsertedCount: 2,
insertedIds: {'0': 4, '4': 7},
upsertedIds: {'1': 5, '2': 6}
},
expected:
[{"_id": 3, "c": 4}, {"_id": 4, "a": 1}, {"_id": 5, "a": 2}, {"_id": 6, "a": 3}]
});
bulkWriteExecutor({
insert: [{_id: 1, c: 1}, {_id: 2, c: 2}, {_id: 3, c: 3}],
params: [
[
{insertOne: {document: {_id: 4, a: 1}}},
{updateOne: {filter: {_id: 5, a: 2}, update: {$set: {a: 2}}, upsert: true}},
{updateMany: {filter: {_id: 6, a: 3}, update: {$set: {a: 3}}, upsert: true}},
{deleteOne: {filter: {c: 1}}},
{deleteMany: {filter: {c: 2}}},
{replaceOne: {filter: {c: 3}, replacement: {c: 4}, upsert: true}}
],
{ordered: false}
],
result: {
acknowledged: true,
insertedCount: 1,
matchedCount: 1,
deletedCount: 2,
upsertedCount: 2,
insertedIds: {'0': 4},
upsertedIds: {'1': 5, '2': 6}
},
expected:
[{"_id": 3, "c": 4}, {"_id": 4, "a": 1}, {"_id": 5, "a": 2}, {"_id": 6, "a": 3}]
});
// DeleteMany
//
// DeleteMany when many documents match
deleteManyExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}],
result: {acknowledged: true, deletedCount: 2},
expected: [{_id: 1, x: 11}]
});
// DeleteMany when no document matches
deleteManyExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}],
result: {acknowledged: true, deletedCount: 0},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// DeleteMany when many documents match, no write concern
deleteManyExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}, {w: 0}],
result: {acknowledged: false},
expected: [{_id: 1, x: 11}]
});
//
// DeleteOne
//
// DeleteOne when many documents match
deleteOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}],
result: {acknowledged: true, deletedCount: 1},
expected: [{_id: 1, x: 11}, {_id: 3, x: 33}]
});
// DeleteOne when one document matches
deleteOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 2}],
result: {acknowledged: true, deletedCount: 1},
expected: [{_id: 1, x: 11}, {_id: 3, x: 33}]
});
// DeleteOne when no documents match
deleteOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}],
result: {acknowledged: true, deletedCount: 0},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// DeleteOne when many documents match, no write concern
deleteOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}, {w: 0}],
result: {acknowledged: false},
expected: [{_id: 1, x: 11}, {_id: 3, x: 33}]
});
//
// FindOneAndDelete
//
// FindOneAndDelete when one document matches
findOneAndDeleteExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 2}}, {projection: {x: 1, _id: 0}, sort: {x: 1}}],
result: {x: 33},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}]
});
// FindOneAndDelete when one document matches
findOneAndDeleteExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 2}, {projection: {x: 1, _id: 0}, sort: {x: 1}}],
result: {x: 22},
expected: [{_id: 1, x: 11}, {_id: 3, x: 33}]
});
// FindOneAndDelete when no documents match
findOneAndDeleteExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {projection: {x: 1, _id: 0}, sort: {x: 1}}],
result: null,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
//
// FindOneAndReplace
//
// FindOneAndReplace when many documents match returning the document before modification
findOneAndReplaceExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}, {x: 32}, {projection: {x: 1, _id: 0}, sort: {x: 1}}],
result: {x: 22},
expected: [{_id: 1, x: 11}, {_id: 2, x: 32}, {_id: 3, x: 33}]
});
// FindOneAndReplace when many documents match returning the document after modification
findOneAndReplaceExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [
{_id: {$gt: 1}},
{x: 32},
{projection: {x: 1, _id: 0}, sort: {x: 1}, returnNewDocument: true}
],
result: {x: 32},
expected: [{_id: 1, x: 11}, {_id: 2, x: 32}, {_id: 3, x: 33}]
});
// FindOneAndReplace when one document matches returning the document before modification
findOneAndReplaceExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 2}, {x: 32}, {projection: {x: 1, _id: 0}, sort: {x: 1}}],
result: {x: 22},
expected: [{_id: 1, x: 11}, {_id: 2, x: 32}, {_id: 3, x: 33}]
});
// FindOneAndReplace when one document matches returning the document after modification
findOneAndReplaceExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [
{_id: 2},
{x: 32},
{projection: {x: 1, _id: 0}, sort: {x: 1}, returnNewDocument: true}
],
result: {x: 32},
expected: [{_id: 1, x: 11}, {_id: 2, x: 32}, {_id: 3, x: 33}]
});
// FindOneAndReplace when no documents match returning the document before modification
findOneAndReplaceExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {x: 44}, {projection: {x: 1, _id: 0}, sort: {x: 1}}],
result: null,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// FindOneAndReplace when no documents match with upsert returning the document before
// modification
findOneAndReplaceExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {x: 44}, {projection: {x: 1, _id: 0}, sort: {x: 1}, upsert: true}],
result: null,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 44}]
});
// FindOneAndReplace when no documents match returning the document after modification
findOneAndReplaceExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [
{_id: 4},
{x: 44},
{projection: {x: 1, _id: 0}, sort: {x: 1}, returnNewDocument: true}
],
result: null,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// FindOneAndReplace when no documents match with upsert returning the document after
// modification
findOneAndReplaceExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [
{_id: 4},
{x: 44},
{projection: {x: 1, _id: 0}, sort: {x: 1}, returnNewDocument: true, upsert: true}
],
result: {x: 44},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 44}]
});
assert.throws(function() {
coll.findOneAndReplace({a: 1}, {$set: {b: 1}});
});
//
// FindOneAndUpdate
//
// FindOneAndUpdate when many documents match returning the document before modification
findOneAndUpdateExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}, {$inc: {x: 1}}, {projection: {x: 1, _id: 0}, sort: {x: 1}}],
result: {x: 22},
expected: [{_id: 1, x: 11}, {_id: 2, x: 23}, {_id: 3, x: 33}]
});
// FindOneAndUpdate when many documents match returning the document after modification
findOneAndUpdateExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [
{_id: {$gt: 1}},
{$inc: {x: 1}},
{projection: {x: 1, _id: 0}, sort: {x: 1}, returnNewDocument: true}
],
result: {x: 23},
expected: [{_id: 1, x: 11}, {_id: 2, x: 23}, {_id: 3, x: 33}]
});
// FindOneAndUpdate when one document matches returning the document before modification
findOneAndUpdateExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 2}, {$inc: {x: 1}}, {projection: {x: 1, _id: 0}, sort: {x: 1}}],
result: {x: 22},
expected: [{_id: 1, x: 11}, {_id: 2, x: 23}, {_id: 3, x: 33}]
});
// FindOneAndUpdate when one document matches returning the document after modification
findOneAndUpdateExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [
{_id: 2},
{$inc: {x: 1}},
{projection: {x: 1, _id: 0}, sort: {x: 1}, returnNewDocument: true}
],
result: {x: 23},
expected: [{_id: 1, x: 11}, {_id: 2, x: 23}, {_id: 3, x: 33}]
});
// FindOneAndUpdate when no documents match returning the document before modification
findOneAndUpdateExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {$inc: {x: 1}}, {projection: {x: 1, _id: 0}, sort: {x: 1}}],
result: null,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// FindOneAndUpdate when no documents match with upsert returning the document before
// modification
findOneAndUpdateExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [
{_id: 4},
{$inc: {x: 1}},
{projection: {x: 1, _id: 0}, sort: {x: 1}, upsert: true}
],
result: null,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 1}]
});
// FindOneAndUpdate when no documents match returning the document after modification
findOneAndUpdateExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [
{_id: 4},
{$inc: {x: 1}},
{projection: {x: 1, _id: 0}, sort: {x: 1}, returnNewDocument: true}
],
result: null,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// FindOneAndUpdate when no documents match with upsert returning the document after
// modification
findOneAndUpdateExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [
{_id: 4},
{$inc: {x: 1}},
{projection: {x: 1, _id: 0}, sort: {x: 1}, returnNewDocument: true, upsert: true}
],
result: {x: 1},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 1}]
});
assert.throws(function() {
coll.findOneAndUpdate({a: 1}, {});
});
assert.throws(function() {
coll.findOneAndUpdate({a: 1}, {b: 1});
});
//
// InsertMany
//
// InsertMany with non-existing documents
insertManyExecutor({
insert: [{_id: 1, x: 11}],
params: [[{_id: 2, x: 22}, {_id: 3, x: 33}]],
result: {acknowledged: true, insertedIds: [2, 3]},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// InsertMany with non-existing documents, no write concern
insertManyExecutor({
insert: [{_id: 1, x: 11}],
params: [[{_id: 2, x: 22}, {_id: 3, x: 33}], {w: 0}],
result: {acknowledged: false},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
//
// InsertOne
//
// InsertOne with non-existing documents
insertOneExecutor({
insert: [{_id: 1, x: 11}],
params: [{_id: 2, x: 22}],
result: {acknowledged: true, insertedId: 2},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}]
});
// InsertOne with non-existing documents, no write concern
insertOneExecutor({
insert: [{_id: 1, x: 11}],
params: [{_id: 2, x: 22}, {w: 0}],
result: {acknowledged: false},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}]
});
//
// ReplaceOne
//
// ReplaceOne when many documents match
replaceOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}, {x: 111}],
result: {acknowledged: true, matchedCount: 1, modifiedCount: 1},
expected: [{_id: 1, x: 11}, {_id: 2, x: 111}, {_id: 3, x: 33}]
});
// ReplaceOne when one document matches
replaceOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 1}, {_id: 1, x: 111}],
result: {acknowledged: true, matchedCount: 1, modifiedCount: 1},
expected: [{_id: 1, x: 111}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// ReplaceOne when no documents match
replaceOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {_id: 4, x: 1}],
result: {acknowledged: true, matchedCount: 0, modifiedCount: 0},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// ReplaceOne with upsert when no documents match without an id specified
replaceOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {x: 1}, {upsert: true}],
result: {acknowledged: true, matchedCount: 0, modifiedCount: 0, upsertedId: 4},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 1}]
});
// ReplaceOne with upsert when no documents match with an id specified
replaceOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {_id: 4, x: 1}, {upsert: true}],
result: {acknowledged: true, matchedCount: 0, modifiedCount: 0, upsertedId: 4},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 1}]
});
// ReplaceOne with upsert when no documents match with an id specified, no write concern
replaceOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {_id: 4, x: 1}, {upsert: true, w: 0}],
result: {acknowledged: false},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 1}]
});
// ReplaceOne with upsert when no documents match with an id specified, no write concern
replaceOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {_id: 4, x: 1}, {upsert: true, writeConcern: {w: 0}}],
result: {acknowledged: false},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 1}]
});
assert.throws(function() {
coll.replaceOne({a: 1}, {$set: {b: 1}});
});
//
// UpdateMany
//
// UpdateMany when many documents match
updateManyExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}, {$inc: {x: 1}}],
result: {acknowledged: true, matchedCount: 2, modifiedCount: 2},
expected: [{_id: 1, x: 11}, {_id: 2, x: 23}, {_id: 3, x: 34}]
});
// UpdateMany when one document matches
updateManyExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 1}, {$inc: {x: 1}}],
result: {acknowledged: true, matchedCount: 1, modifiedCount: 1},
expected: [{_id: 1, x: 12}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// UpdateMany when no documents match
updateManyExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {$inc: {x: 1}}],
result: {acknowledged: true, matchedCount: 0, modifiedCount: 0},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// UpdateMany with upsert when no documents match
updateManyExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {$inc: {x: 1}}, {upsert: true}],
result: {acknowledged: true, matchedCount: 0, modifiedCount: 0, upsertedId: 4},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 1}]
});
// UpdateMany with upsert when no documents match, no write concern
updateManyExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {$inc: {x: 1}}, {upsert: true, w: 0}],
result: {acknowledged: false},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 1}]
});
assert.throws(function() {
coll.updateMany({a: 1}, {});
});
assert.throws(function() {
coll.updateMany({a: 1}, {b: 1});
});
//
// UpdateOne
//
// UpdateOne when many documents match
updateOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}, {$inc: {x: 1}}],
result: {acknowledged: true, matchedCount: 1, modifiedCount: 1},
expected: [{_id: 1, x: 11}, {_id: 2, x: 23}, {_id: 3, x: 33}]
});
// UpdateOne when one document matches
updateOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 1}, {$inc: {x: 1}}],
result: {acknowledged: true, matchedCount: 1, modifiedCount: 1},
expected: [{_id: 1, x: 12}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// UpdateOne when no documents match
updateOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {$inc: {x: 1}}],
result: {acknowledged: true, matchedCount: 0, modifiedCount: 0},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// UpdateOne with upsert when no documents match
updateOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: 4}, {$inc: {x: 1}}, {upsert: true}],
result: {acknowledged: true, matchedCount: 0, modifiedCount: 0, upsertedId: 4},
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}, {_id: 4, x: 1}]
});
// UpdateOne when many documents match, no write concern
updateOneExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}, {$inc: {x: 1}}, {w: 0}],
result: {acknowledged: false},
expected: [{_id: 1, x: 11}, {_id: 2, x: 23}, {_id: 3, x: 33}]
});
assert.throws(function() {
coll.updateOne({a: 1}, {});
});
assert.throws(function() {
coll.updateOne({a: 1}, {b: 1});
});
//
// Count
//
// Simple count of all elements
countExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{}],
result: 3,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// Simple count no arguments
countExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [],
result: 3,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// Simple count filtered
countExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{_id: {$gt: 1}}],
result: 2,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// Simple count of all elements, applying limit
countExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{}, {limit: 1}],
result: 1,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// Simple count of all elements, applying skip
countExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{}, {skip: 1}],
result: 2,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// Simple count no arguments, applying hint
countExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: [{}, {hint: {"_id": 1}}],
result: 3,
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
//
// Distinct
//
// Simple distinct of field x no filter
distinctExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: ['x'],
result: [11, 22, 33],
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// Simple distinct of field x
distinctExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: ['x', {}],
result: [11, 22, 33],
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// Simple distinct of field x filtered
distinctExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: ['x', {x: {$gt: 11}}],
result: [22, 33],
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
// Simple distinct of field x filtered with maxTimeMS
distinctExecutor({
insert: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}],
params: ['x', {x: {$gt: 11}}, {maxTimeMS: 100000}],
result: [22, 33],
expected: [{_id: 1, x: 11}, {_id: 2, x: 22}, {_id: 3, x: 33}]
});
//
// Find
//
coll.deleteMany({});
// Insert all of them
coll.insertMany([{a: 0, b: 0}, {a: 1, b: 1}]);
// Simple projection
var result =
coll.find({}).sort({a: 1}).limit(1).skip(1).projection({_id: 0, a: 1}).toArray();
assert.docEq(result, [{a: 1}]);
// Simple tailable cursor
var cursor = coll.find({}).sort({a: 1}).tailable();
assert.eq(34, (cursor._options & ~DBQuery.Option.slaveOk));
var cursor = coll.find({}).sort({a: 1}).tailable(false);
assert.eq(2, (cursor._options & ~DBQuery.Option.slaveOk));
// Check modifiers
var cursor = coll.find({}).modifiers({$hint: 'a_1'});
assert.eq('a_1', cursor._query['$hint']);
// allowPartialResults
var cursor = coll.find({}).allowPartialResults();
assert.eq(128, (cursor._options & ~DBQuery.Option.slaveOk));
// noCursorTimeout
var cursor = coll.find({}).noCursorTimeout();
assert.eq(16, (cursor._options & ~DBQuery.Option.slaveOk));
// oplogReplay
var cursor = coll.find({}).oplogReplay();
assert.eq(8, (cursor._options & ~DBQuery.Option.slaveOk));
//
// Aggregation
//
coll.deleteMany({});
// Insert all of them
coll.insertMany([{a: 0, b: 0}, {a: 1, b: 1}]);
// Simple aggregation with useCursor
var result = coll.aggregate([{$match: {}}], {useCursor: true}).toArray();
assert.eq(2, result.length);
// Simple aggregation with batchSize
var result = coll.aggregate([{$match: {}}], {batchSize: 2}).toArray();
assert.eq(2, result.length);
// Drop collection
coll.drop();
coll.ensureIndex({a: 1}, {unique: true});
// Should throw duplicate key error
assert.throws(function() {
coll.insertMany([{a: 0, b: 0}, {a: 0, b: 1}]);
});
assert(coll.findOne({a: 0, b: 0}) != null);
assert.throws(function() {
coll.insertOne({a: 0, b: 0});
});
assert.throws(function() {
coll.updateOne({b: 2}, {$set: {a: 0}}, {upsert: true});
});
assert.throws(function() {
coll.updateMany({b: 2}, {$set: {a: 0}}, {upsert: true});
});
assert.throws(function() {
coll.deleteOne({$invalidFieldName: {a: 1}});
});
assert.throws(function() {
coll.deleteMany({$set: {a: 1}});
});
assert.throws(function() {
coll.bulkWrite([{insertOne: {document: {_id: 4, a: 0}}}]);
});
};
crudAPISpecTests();
})();
|