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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
|
/**
* Test updates into sharded timeseries collection.
*
* @tags: [
* requires_fcv_51,
* ]
*/
(function() {
"use strict";
load("jstests/core/timeseries/libs/timeseries.js"); // For 'TimeseriesTest' helpers.
const st = new ShardingTest({shards: 2, rs: {nodes: 2}});
const mongos = st.s0;
//
// Constants used throughout all tests.
//
const dbName = 'testDB';
const collName = 'coll';
const timeField = "time";
const metaField = "tag";
const dateTime = ISODate("2021-07-12T16:00:00Z");
//
// Checks for feature flags.
//
if (!TimeseriesTest.shardedtimeseriesCollectionsEnabled(st.shard0)) {
jsTestLog("Skipping test because the sharded time-series collection feature flag is disabled");
st.stop();
return;
}
const testDB = mongos.getDB(dbName);
testDB.dropDatabase();
assert.commandWorked(mongos.adminCommand({enableSharding: dbName}));
if (!TimeseriesTest.shardedTimeseriesUpdatesAndDeletesEnabled(st.shard0)) {
// Ensure that the feature flag correctly prevents us from running an update on a sharded
// timeseries collection.
assert.commandWorked(testDB.createCollection(collName, {timeseries: {timeField, metaField}}));
const coll = testDB.getCollection(collName);
assert.commandWorked(coll.createIndex({[timeField]: 1}));
assert.commandWorked(mongos.adminCommand({
shardCollection: `${dbName}.${collName}`,
key: {[timeField]: 1},
}));
assert.commandFailedWithCode(
testDB.runCommand(
{update: coll.getName(), updates: [{q: {}, u: {[metaField]: 1}, multi: true}]}),
[ErrorCodes.NotImplemented, ErrorCodes.InvalidOptions]);
st.stop();
return;
}
const arbitraryUpdatesEnabled = TimeseriesTest.arbitraryUpdatesEnabled(st.shard0);
const doc1 = {
_id: 1,
[timeField]: dateTime,
[metaField]: {a: "A", b: "B"}
};
const doc2 = {
_id: 2,
[timeField]: dateTime,
[metaField]: {c: "C", d: 2},
f: [{"k": "K", "v": "V"}]
};
const doc3 = {
_id: 3,
[timeField]: dateTime,
f: "F"
};
const doc4 = {
_id: 4,
[timeField]: dateTime,
[metaField]: {a: "A", b: "B"},
f: "F"
};
const doc5 = {
_id: 5,
[timeField]: dateTime,
[metaField]: {a: "A", b: "B", c: "C"}
};
//
// Helper functions.
//
/**
* Confirms that a set of updates returns the expected set of documents.
*/
function testShardedUpdate({
insert,
shardKey,
updatesMetaFieldInShardKey,
timeseries,
initialDocList,
updates,
resultDocList,
n,
nModified = n,
letDoc = {},
failCode,
ordered = true,
}) {
assert.commandWorked(mongos.adminCommand({enableSharding: dbName}));
assert.commandWorked(testDB.createCollection(collName, {timeseries}));
const coll = testDB.getCollection(collName);
assert.commandWorked(coll.createIndex(shardKey));
assert.commandWorked(insert(coll, initialDocList));
assert.commandWorked(mongos.adminCommand({
shardCollection: `${dbName}.${collName}`,
key: shardKey,
}));
// Updates on timeseries collections are not allowed if no metaField is defined.
if (!timeseries["metaField"]) {
failCode = [ErrorCodes.InvalidOptions];
n = 0;
nModified = 0;
resultDocList = initialDocList;
}
// Updates on sharded timeseries meta fields are only allowed as long as the field updated is
// not in the shard key.
if (updatesMetaFieldInShardKey) {
failCode = [ErrorCodes.InvalidOptions, 31025];
n = 0;
nModified = 0;
resultDocList = initialDocList;
}
const updateCommand = {update: coll.getName(), updates, ordered, let : letDoc};
const res = failCode ? assert.commandFailedWithCode(testDB.runCommand(updateCommand), failCode)
: assert.commandWorked(testDB.runCommand(updateCommand));
assert.eq(n, res.n);
assert.eq(nModified, res.nModified);
assert.eq(initialDocList.length, resultDocList.length);
resultDocList.forEach(resultDoc => {
assert.docEq(
resultDoc,
coll.findOne({_id: resultDoc._id}),
"Expected document not found in result collection:" + tojson(coll.find().toArray()));
});
assert(coll.drop());
}
/**
* Wrapper on testShardedUpdate to compose tests with a given shardKey/timeseries combo.
*/
function testUpdates({shardKeyTimeField, shardKeyMetaFieldPath, timeseriesOptions, tests}) {
// Set up the shard key for the tests.
let shardKey = {};
if (shardKeyMetaFieldPath) {
shardKey[shardKeyMetaFieldPath] = 1;
}
if (shardKeyTimeField) {
shardKey[shardKeyTimeField] = 1;
}
// Run a series of updates.
TimeseriesTest.run((insert) => {
// Helper lambda which has the extensions field set to:
// - undefined if we are updating the meta field.
// - the name of a subfield of meta we are updating.
// - an array of names of subfields of meta we are updating.
const checkUpdatesMetaFieldInShardKey = (pathToMetaFieldBeingUpdated) => {
if ((pathToMetaFieldBeingUpdated === undefined) || !shardKeyMetaFieldPath) {
// If we do not have the meta field in the shard key, we are able to update it.
return false;
} else if ((shardKeyMetaFieldPath === metaField) ||
(pathToMetaFieldBeingUpdated === "")) {
// If the top-level meta field is in the shard key, we cannot update it.
return true;
} else if (!Array.isArray(pathToMetaFieldBeingUpdated)) {
pathToMetaFieldBeingUpdated = [pathToMetaFieldBeingUpdated];
}
for (const e of pathToMetaFieldBeingUpdated) {
if (metaField + "." + e === shardKeyMetaFieldPath) {
return true;
}
}
return false;
};
const testUpdate = (fields, additionalFields = {}) => {
let inputs = Object.assign({}, fields, additionalFields);
testShardedUpdate(Object.assign({}, inputs, {
shardKey,
insert,
timeseries: timeseriesOptions,
updatesMetaFieldInShardKey:
checkUpdatesMetaFieldInShardKey(inputs.pathToMetaFieldBeingUpdated)
}));
};
tests.forEach(test => test({testUpdate}));
}, testDB);
}
/**
* Helper function to generate the parameters to pass to 'testUpdates' when an update is expected to
* fail.
*/
function expectFailedUpdate(initialDocList) {
return {
initialDocList,
resultDocList: initialDocList,
n: 0,
failCode: ErrorCodes.InvalidOptions,
};
}
//
// Test cases when the update command fails.
//
function testCaseMultiFalseUpdateFails({testUpdate}) {
testUpdate({updates: [{q: {[metaField]: {b: "B"}}, u: {$set: {[metaField]: {b: "C"}}}}]},
expectFailedUpdate([doc1]));
}
function testCaseReplacementAndPipelineUpdateFails({testUpdate}) {
if (arbitraryUpdatesEnabled) {
return;
}
const expectFailedUpdateDoc = expectFailedUpdate([doc2]);
// Replace a document to have no metaField, which should fail since updates with replacement
// documents are not supported.
testUpdate({
updates: [{
q: {[metaField]: {c: "C", d: 2}},
u: {f2: {e: "E", f: "F"}, f3: 7},
multi: true,
}]
},
expectFailedUpdateDoc);
// Replace a document with an empty document, which should fail since updates with replacement
// documents are not supported.
testUpdate({
updates: [{
q: {[metaField]: {c: "C", d: 2}},
u: {},
multi: true,
}]
},
expectFailedUpdateDoc);
// Modify the metaField, which should fail since pipeline-style updates are not supported.
testUpdate({
updates: [{
q: {},
u: [
{$addFields: {[metaField + ".c"]: "C", [metaField + ".e"]: "E"}},
{$unset: metaField + ".e"}
],
multi: true,
}]
},
expectFailedUpdateDoc);
}
function testCaseNoMetaFieldQueryUpdateFails({testUpdate}) {
if (arbitraryUpdatesEnabled) {
return;
}
// Query on a field which is not the (nonexistent) metaField.
testUpdate({
updates: [{
q: {f: "F"},
u: {},
multi: true,
}]
},
expectFailedUpdate([doc3]));
// Query on all documents and update them to be empty documents.
testUpdate({
updates: [{
q: {},
u: {},
multi: true,
}]
},
expectFailedUpdate([doc3]));
// Query on all documents and update them to be non-empty documents.
testUpdate({
updates: [{
q: {},
u: {f: "FF"},
multi: true,
}]
},
expectFailedUpdate([doc3]));
// Query on a field that is not the metaField.
testUpdate({
updates: [{
q: {measurement: "cpu"},
u: {$set: {[metaField]: {c: "C"}}},
multi: true,
}]
},
expectFailedUpdate([doc1]));
// Query on the metaField and a field that is not the metaField.
testUpdate({
updates: [
{
q: {[metaField]: {a: "A", b: "B"}, measurement: "cpu"},
u: {$set: {[metaField]: {c: "C"}}},
multi: true,
},
]
},
expectFailedUpdate([doc1]));
// Query on a field that is not the metaField using dot notation and modify the metaField.
testUpdate({
updates: [{
q: {"measurement.A": "cpu"},
u: {$set: {[metaField]: {c: "C"}}},
multi: true,
}]
},
expectFailedUpdate([doc1]));
}
function testCaseIllegalMetaFieldUpdateFails({testUpdate}) {
if (arbitraryUpdatesEnabled) {
return;
}
// Query on the metaField and modify a field that is not the metaField.
testUpdate({
updates: [{
q: {[metaField]: {c: "C", d: 2}},
u: {$set: {f2: "f2"}},
multi: true,
}]
},
expectFailedUpdate([doc2]));
// Query on the metaField and modify the metaField and fields that are not the metaField.
testUpdate({
updates: [{
q: {[metaField]: {c: "C", d: 2}},
u: {$set: {[metaField]: {e: "E"}, f3: "f3"}, $inc: {f2: 3}, $unset: {f1: ""}},
multi: true,
}]
},
expectFailedUpdate([doc2]));
// Rename the metaField.
testUpdate({
updates: [{
q: {[metaField + ".a"]: "A"},
u: {$rename: {[metaField]: "Z"}},
multi: true,
}]
},
expectFailedUpdate([doc1, doc2, doc4]));
// Rename a subfield of the metaField to something not in the metaField.
testUpdate({
updates: [{
q: {[metaField + ".a"]: "A"},
u: {$rename: {[metaField + ".a"]: "notMetaField.a"}},
multi: true,
}]
},
expectFailedUpdate([doc1, doc2, doc4]));
}
//
// Test cases when the update command succeeds.
//
function testCaseBatchUpdates({testUpdate}) {
// Multiple updates, ordered: query on the metaField and modify the metaField multiple times.
testUpdate({
initialDocList: [doc2],
updates: [
{
q: {[metaField]: {c: "C", d: 2}},
u: {$set: {[metaField]: 1}},
multi: true,
},
{
q: {[metaField]: 1},
u: {$set: {[metaField]: 2}},
multi: true,
},
{
q: {[metaField]: 2},
u: {$set: {[metaField]: 3}},
multi: true,
}
],
resultDocList: [{
_id: 2,
[timeField]: dateTime,
[metaField]: 3,
f: [{"k": "K", "v": "V"}],
}],
n: 3,
pathToMetaFieldBeingUpdated: "",
});
// Multiple updates, ordered: query on the metaField and on a field that is not the metaField.
testUpdate({
initialDocList: [doc1],
updates: [
{
q: {[metaField]: {a: "A", b: "B"}},
u: {$set: {[metaField]: {c: "C", d: 1}}},
multi: true,
},
{
q: {measurement: "cpu", [metaField + ".d"]: 1},
u: {$set: {[metaField + ".c"]: "CC"}},
multi: true,
}
],
resultDocList: [doc1],
// If the shardKey is on one of the fields being updated, this must fail.
n: 1,
pathToMetaFieldBeingUpdated: "",
failCode: ErrorCodes.InvalidOptions,
});
// Multiple updates, ordered: query on the metaField and modify the metaField and a field that
// is not the metaField using dot notation.
testUpdate({
initialDocList: [doc2],
updates: [
{
q: {[metaField]: {c: "C", d: 2}},
u: {$inc: {[metaField + ".d"]: 6}},
multi: true,
},
{
q: {[metaField]: {c: "C", d: 8}},
u: {$set: {"f1.0": "f2"}},
multi: true,
}
],
resultDocList: [{
_id: 2,
[timeField]: dateTime,
[metaField]: {c: "C", d: 8},
f: [{"k": "K", "v": "V"}],
}],
// If the shardKey is on the field being updated, this must fail to update any docs.
n: 1,
pathToMetaFieldBeingUpdated: "d",
failCode: ErrorCodes.InvalidOptions,
});
// Multiple updates, ordered: query on the metaField and modify a field that is not the
// metaField using dot notation.
if (!arbitraryUpdatesEnabled) {
testUpdate({
updates: [
{
q: {[metaField]: {c: "C", d: 2}},
u: {$set: {"f1.0": "f2"}},
multi: true,
},
{
q: {[metaField]: {c: "C", d: 2}},
u: {$inc: {[metaField + ".d"]: 6}},
multi: true,
}
]
},
expectFailedUpdate([doc2]));
// Multiple updates, unordered: Modify the metaField, a field that is not the metaField, and
// the metaField. The first and last updates should succeed.
testUpdate({
initialDocList: [doc2],
updates: [
{
q: {[metaField]: {c: "C", d: 2}},
u: {$inc: {[metaField + ".d"]: 6}},
multi: true,
},
{
q: {[metaField]: {c: "C", d: 8}},
u: {$set: {"f1.0": "f2"}},
multi: true,
},
{
q: {[metaField]: {c: "C", d: 8}},
u: {$inc: {[metaField + ".d"]: 7}},
multi: true,
}
],
resultDocList: [{
_id: 2,
[timeField]: dateTime,
[metaField]: {c: "C", d: 15},
f: [{"k": "K", "v": "V"}],
}],
ordered: false,
n: 2,
pathToMetaFieldBeingUpdated: "d",
failCode: ErrorCodes.InvalidOptions,
});
}
}
function testCaseValidMetaFieldUpdates({testUpdate}) {
// Rename a subfield to the metaField.
testUpdate({
initialDocList: [doc1, doc2],
updates: [{
q: {[metaField + ".a"]: "A"},
u: {$rename: {[metaField + ".a"]: metaField + ".z"}},
multi: true,
}],
resultDocList: [{_id: 1, [timeField]: dateTime, [metaField]: {z: "A", b: "B"}}, doc2],
n: 1,
pathToMetaFieldBeingUpdated: "a",
});
// Query on the metaField and modify the metaField.
testUpdate({
initialDocList: [doc1],
updates: [{
q: {[metaField]: {a: "A", b: "B"}},
u: {$set: {[metaField]: {c: "C"}}},
multi: true,
}],
resultDocList: [{_id: 1, [timeField]: dateTime, [metaField]: {c: "C"}}],
n: 1,
pathToMetaFieldBeingUpdated: "",
});
// Query on the metaField and modify the metaField of 1 matching document.
testUpdate({
initialDocList: [doc1, doc2, doc4, doc5],
updates: [{
q: {"$and": [{[metaField + ".c"]: "C"}, {[metaField + ".d"]: 2}]},
u: {$set: {[metaField + ".c"]: 1}},
multi: true,
}],
resultDocList: [
doc1,
{_id: 2, [timeField]: dateTime, [metaField]: {c: 1, d: 2}, f: [{"k": "K", "v": "V"}]},
doc4,
doc5
],
ordered: false,
n: 1,
pathToMetaFieldBeingUpdated: "c",
});
// Query on the metaField and update the metaField of multiple matching documents.
testUpdate({
initialDocList: [doc1, doc2, doc4, doc5],
updates: [{
q: {[metaField + ".a"]: "A"},
u: {$unset: {[metaField + ".a"]: ""}},
multi: true,
}],
resultDocList: [
{_id: 1, [timeField]: dateTime, [metaField]: {b: "B"}},
{_id: 2, [timeField]: dateTime, [metaField]: {c: "C", d: 2}, f: [{"k": "K", "v": "V"}]},
{_id: 4, [timeField]: dateTime, [metaField]: {b: "B"}, f: "F"},
{_id: 5, [timeField]: dateTime, [metaField]: {b: "B", c: "C"}}
],
ordered: false,
n: 3,
pathToMetaFieldBeingUpdated: "a",
});
// Compound query on the metaField using dot notation and modify the metaField.
testUpdate({
initialDocList: [doc1],
updates: [{
q: {"$and": [{[metaField + ".a"]: "A"}, {[metaField + ".b"]: "B"}]},
u: {$set: {[metaField]: {c: "C"}}},
multi: true,
}],
resultDocList: [{_id: 1, [timeField]: dateTime, [metaField]: {c: "C"}}],
n: 1,
pathToMetaFieldBeingUpdated: "",
});
// Query on the metaField using dot notation and modify the metaField.
testUpdate({
initialDocList: [doc1],
updates: [{
q: {[metaField + ".a"]: "A"},
u: {$set: {[metaField]: {c: "C"}}},
multi: true,
}],
resultDocList: [{_id: 1, [timeField]: dateTime, [metaField]: {c: "C"}}],
n: 1,
pathToMetaFieldBeingUpdated: "",
});
// Query on the metaField using dot notation and modify the metaField.
testUpdate({
initialDocList: [doc2],
updates: [{
q: {[metaField + ".c"]: "C"},
u: {$inc: {[metaField + ".d"]: 10}},
multi: true,
}],
resultDocList: [
{_id: 2, [timeField]: dateTime, [metaField]: {c: "C", d: 12}, f: [{"k": "K", "v": "V"}]}
],
n: 1,
pathToMetaFieldBeingUpdated: "d",
});
// Query with an empty document (i.e update all documents in the collection).
testUpdate({
initialDocList: [doc1, doc2],
updates: [{
q: {},
u: {$set: {[metaField]: {z: "Z"}}},
multi: true,
}],
resultDocList: [
{_id: 1, [timeField]: dateTime, [metaField]: {z: "Z"}},
{_id: 2, [timeField]: dateTime, [metaField]: {z: "Z"}, f: [{"k": "K", "v": "V"}]}
],
n: 2,
pathToMetaFieldBeingUpdated: "",
});
// Remove the metaField.
testUpdate({
initialDocList: [doc1],
updates:
[{q: {[metaField]: {a: "A", b: "B"}}, u: {$unset: {[metaField]: ""}}, multi: true}],
resultDocList: [{_id: 1, [timeField]: dateTime}],
n: 1,
pathToMetaFieldBeingUpdated: "",
});
// Update where one of the matching documents is a no-op update.
testUpdate({
initialDocList: [doc1, doc4, doc5],
updates: [
{
q: {[metaField + ".a"]: "A"},
u: {$set: {[metaField + ".c"]: "C"}},
multi: true,
},
],
resultDocList: [
{_id: 1, [timeField]: dateTime, [metaField]: {a: "A", b: "B", c: "C"}},
{_id: 4, [timeField]: dateTime, [metaField]: {a: "A", b: "B", c: "C"}, f: "F"},
{_id: 5, [timeField]: dateTime, [metaField]: {a: "A", b: "B", c: "C"}}
],
n: 3,
nModified: 2,
pathToMetaFieldBeingUpdated: "c",
});
// Query for documents using $jsonSchema with the metaField required.
testUpdate({
initialDocList: [doc1, doc2, doc3],
updates: [{
q: {"$jsonSchema": {"required": [metaField]}},
u: {$set: {[metaField]: "a"}},
multi: true
}],
resultDocList: [
{_id: 1, [timeField]: dateTime, [metaField]: "a"},
{_id: 2, [timeField]: dateTime, [metaField]: "a", f: [{"k": "K", "v": "V"}]},
doc3
],
n: 2,
pathToMetaFieldBeingUpdated: "",
});
// Query for documents using $jsonSchema with the metaField in dot notation required.
testUpdate({
initialDocList: [doc1, doc2, doc3],
updates: [{
q: {"$jsonSchema": {"required": [metaField + ".a"]}},
u: {$set: {[metaField]: "a"}},
multi: true
}],
resultDocList: [{_id: 1, [timeField]: dateTime, [metaField]: "a"}, doc2, doc3],
n: 1,
pathToMetaFieldBeingUpdated: "",
});
// Query for documents using $jsonSchema with a field that is not the metaField required.
testUpdate({
updates: [{
q: {"$jsonSchema": {"required": [metaField, timeField]}},
u: {$set: {[metaField]: "a"}},
multi: true
}],
},
expectFailedUpdate([doc1, doc2, doc3]));
const nestedMetaObj = {_id: 6, [timeField]: dateTime, [metaField]: {[metaField]: "A", a: 1}};
// Query for documents using $jsonSchema with the metaField required and a required subfield of
// the metaField with the same name as the metaField.
if (!arbitraryUpdatesEnabled) {
testUpdate({
initialDocList: [doc1, nestedMetaObj],
updates: [{
q: {
"$jsonSchema": {
"required": [metaField],
"properties": {[metaField]: {"required": [metaField]}}
}
},
u: {$set: {[metaField]: "a"}},
multi: true
}],
resultDocList: [doc1, {_id: 6, [timeField]: dateTime, [metaField]: "a", a: 1}],
n: 1,
pathToMetaFieldBeingUpdated: "",
});
}
// Query for documents using $jsonSchema with the metaField required and an optional field that
// is not the metaField.
testUpdate({
updates: [{
q: {
"$jsonSchema": {
"required": [metaField],
"properties": {"measurement": {description: "can be any value"}}
}
},
u: {$set: {[metaField]: "a"}},
multi: true
}]
},
expectFailedUpdate([doc1, nestedMetaObj]));
// Query for documents on the metaField with the metaField nested within nested operators.
testUpdate({
initialDocList: [doc1, doc2, doc3],
updates: [{
q: {
"$and": [
{"$or": [{[metaField]: {"$ne": "B"}}, {[metaField]: {"a": {"$eq": "B"}}}]},
{[metaField]: {a: "A", b: "B"}}
]
},
u: {$set: {[metaField]: "a"}},
multi: true
}],
resultDocList: [{_id: 1, [timeField]: dateTime, [metaField]: "a"}, doc2, doc3],
n: 1,
pathToMetaFieldBeingUpdated: "",
});
// Updates where upsert:false should not insert a new document when no match is found. In case
// the shard key is on the meta field, this should not update any documents but also but not
// report a failure, since no documents were matched.
testUpdate({
initialDocList: [doc1, doc4, doc5],
updates: [{
q: {[metaField]: "Z"},
u: {$set: {[metaField]: 5}},
multi: true,
}],
resultDocList: [doc1, doc4, doc5],
n: 0,
});
// Do the same test case as above but with upsert:true, which should fail.
testUpdate({
updates: [{
q: {[metaField]: "Z"},
u: {$set: {[metaField]: 5}},
multi: true,
upsert: true,
}]
},
expectFailedUpdate([doc1, doc4, doc5]));
}
function testCaseUpdateWithLetDoc({testUpdate}) {
// Use a variable defined in the let option in the query to modify the metaField.
testUpdate({
initialDocList: [doc1, doc4, doc5],
updates: [{
q: {$expr: {$eq: ["$" + metaField + ".a", "$$oldVal"]}},
u: {$set: {[metaField]: "aaa"}},
multi: true,
}],
letDoc: {oldVal: "A"},
resultDocList: [
{_id: 1, [timeField]: dateTime, [metaField]: "aaa"},
{_id: 4, [timeField]: dateTime, [metaField]: "aaa", f: "F"},
{_id: 5, [timeField]: dateTime, [metaField]: "aaa"}
],
n: 3,
pathToMetaFieldBeingUpdated: "",
});
// Variables defined in the let option can only be used in the update if the update is an
// pipeline update. Since this update is an update document, the literal name of the variable
// will be used in the update instead of the variable's value.
testUpdate({
initialDocList: [doc1],
updates: [{
q: {[metaField + ".a"]: "A"},
u: {$set: {[metaField]: "$$myVar"}},
multi: true,
}],
letDoc: {myVar: "aaa"},
resultDocList: [{_id: 1, [timeField]: dateTime, [metaField]: "$$myVar"}],
n: 1,
pathToMetaFieldBeingUpdated: "",
});
// Use variables defined in the let option in the query to modify the metaField multiple times.
testUpdate({
initialDocList: [doc1, doc4, doc5],
updates: [
{
q: {$expr: {$eq: ["$" + metaField + ".a", "$$val1"]}},
u: {$set: {[metaField]: "aaa"}},
multi: true,
},
{
q: {$expr: {$eq: ["$" + metaField, "$$val2"]}},
u: {$set: {[metaField]: "bbb"}},
multi: true,
}
],
letDoc: {val1: "A", val2: "aaa"},
resultDocList: [
{_id: 1, [timeField]: dateTime, [metaField]: "bbb"},
{_id: 4, [timeField]: dateTime, [metaField]: "bbb", f: "F"},
{_id: 5, [timeField]: dateTime, [metaField]: "bbb"}
],
n: 6,
pathToMetaFieldBeingUpdated: "",
});
}
function testCaseCollationUpdates({testUpdate}) {
const collationDoc1 = {_id: 1, [timeField]: dateTime, [metaField]: "café"};
const collationDoc2 = {_id: 2, [timeField]: dateTime, [metaField]: "cafe"};
const collationDoc3 = {_id: 3, [timeField]: dateTime, [metaField]: "cafE"};
const initialDocList = [collationDoc1, collationDoc2, collationDoc3];
// Query on the metaField and modify the metaField using collation with strength level 1.
testUpdate({
initialDocList,
updates: [{
q: {[metaField]: "cafe"},
u: {$set: {[metaField]: "Updated"}},
multi: true,
collation: {locale: "fr", strength: 1},
}],
resultDocList: [
{_id: 1, [timeField]: dateTime, [metaField]: "Updated"},
{_id: 2, [timeField]: dateTime, [metaField]: "Updated"},
{_id: 3, [timeField]: dateTime, [metaField]: "Updated"}
],
n: 3,
pathToMetaFieldBeingUpdated: "",
});
// Query on the metaField and modify the metaField using collation with the default strength
// (level 3).
testUpdate({
initialDocList,
updates: [{
q: {[metaField]: "cafe"},
u: {$set: {[metaField]: "Updated"}},
multi: true,
collation: {locale: "fr"},
}],
resultDocList: [
collationDoc1,
{_id: 2, [timeField]: dateTime, [metaField]: "Updated"},
collationDoc3,
],
n: 1,
pathToMetaFieldBeingUpdated: "",
});
}
function testCaseNullUpdates({testUpdate}) {
// Assumes shard key is meta.a.
const nullDoc = {_id: 1, [timeField]: dateTime, [metaField]: {a: null, b: 1}};
const missingDoc1 = {_id: 2, [timeField]: dateTime, [metaField]: {b: 1}};
const missingDoc2 = {_id: 3, [timeField]: dateTime, [metaField]: "foo"};
const initialDocList = [nullDoc, missingDoc1, missingDoc2];
// Query on the metaField and modify the metaField using collation with strength level 1.
testUpdate({
initialDocList,
updates: [{
q: {[metaField]: {$ne: null}},
u: {$set: {[metaField]: "Updated"}},
multi: true,
}],
resultDocList: [
{_id: 1, [timeField]: dateTime, [metaField]: "Updated"},
{_id: 2, [timeField]: dateTime, [metaField]: "Updated"},
{_id: 3, [timeField]: dateTime, [metaField]: "Updated"},
],
n: 3,
});
}
// Run tests with a variety of shardKeys and timeseries configurations.
const timeseriesOptions = {
timeField,
metaField
};
const tests = [
testCaseMultiFalseUpdateFails,
testCaseNoMetaFieldQueryUpdateFails,
testCaseIllegalMetaFieldUpdateFails,
testCaseReplacementAndPipelineUpdateFails,
testCaseCollationUpdates,
testCaseUpdateWithLetDoc,
testCaseBatchUpdates,
testCaseValidMetaFieldUpdates,
];
if (!arbitraryUpdatesEnabled) {
testUpdates({shardKeyTimeField: timeField, timeseriesOptions: {timeField}, tests});
}
testUpdates({shardKeyMetaFieldPath: metaField, timeseriesOptions, tests});
testUpdates(
{shardKeyTimeField: timeField, shardKeyMetaFieldPath: metaField, timeseriesOptions, tests});
// Run a relevant subset of tests in the case when meta.a is the shard key.
const testsForMetaSubfieldShardKey = [
testCaseNullUpdates,
testCaseMultiFalseUpdateFails,
testCaseNoMetaFieldQueryUpdateFails,
testCaseIllegalMetaFieldUpdateFails,
testCaseReplacementAndPipelineUpdateFails,
testCaseValidMetaFieldUpdates,
];
testUpdates({
shardKeyMetaFieldPath: metaField + ".a",
timeseriesOptions,
tests: testsForMetaSubfieldShardKey,
});
testUpdates({
shardKeyMetaFieldPath: metaField + ".a",
shardKeyTimeField: timeField,
timeseriesOptions,
tests: testsForMetaSubfieldShardKey,
});
st.stop();
})();
|