summaryrefslogtreecommitdiff
path: root/jstests/sharding/query/collation_targeting_inherited.js
blob: 4cc9e23765dbbe5107e6f221ee6d5ba6705bedc7 (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
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
// Test shard targeting for queries on a collection with a default collation.
(function() {
"use strict";

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

const caseInsensitive = {
    locale: "en_US",
    strength: 2
};

var explain;
var writeRes;

// Create a cluster with 3 shards.
var st = new ShardingTest({shards: 3});
var testDB = st.s.getDB("test");
assert.commandWorked(testDB.adminCommand({enableSharding: testDB.getName()}));
st.ensurePrimaryShard(testDB.getName(), st.shard1.shardName);

// Create a collection with a case-insensitive default collation sharded on {a: 1}.
var collCaseInsensitive = testDB.getCollection("case_insensitive");
collCaseInsensitive.drop();
assert.commandWorked(testDB.createCollection("case_insensitive", {collation: caseInsensitive}));
assert.commandWorked(collCaseInsensitive.createIndex({a: 1}, {collation: {locale: "simple"}}));
assert.commandWorked(collCaseInsensitive.createIndex({geo: "2dsphere"}));
assert.commandWorked(testDB.adminCommand({
    shardCollection: collCaseInsensitive.getFullName(),
    key: {a: 1},
    collation: {locale: "simple"}
}));

// Split the collection.
// st.shard0.shardName: { "a" : { "$minKey" : 1 } } -->> { "a" : 10 }
// st.shard1.shardName: { "a" : 10 } -->> { "a" : "a"}
// shard0002: { "a" : "a" } -->> { "a" : { "$maxKey" : 1 }}
assert.commandWorked(
    testDB.adminCommand({split: collCaseInsensitive.getFullName(), middle: {a: 10}}));
assert.commandWorked(
    testDB.adminCommand({split: collCaseInsensitive.getFullName(), middle: {a: "a"}}));
assert.commandWorked(testDB.adminCommand(
    {moveChunk: collCaseInsensitive.getFullName(), find: {a: 1}, to: st.shard0.shardName}));
assert.commandWorked(testDB.adminCommand(
    {moveChunk: collCaseInsensitive.getFullName(), find: {a: "FOO"}, to: st.shard1.shardName}));
assert.commandWorked(testDB.adminCommand(
    {moveChunk: collCaseInsensitive.getFullName(), find: {a: "foo"}, to: st.shard2.shardName}));

// Put data on each shard.
// Note that the balancer is off by default, so the chunks will stay put.
// st.shard0.shardName: {a: 1}
// st.shard1.shardName: {a: 100}, {a: "FOO"}
// shard0002: {a: "foo"}
// Include geo field to test geoNear.
var a_1 = {_id: 0, a: 1, geo: {type: "Point", coordinates: [0, 0]}};
var a_100 = {_id: 1, a: 100, geo: {type: "Point", coordinates: [0, 0]}};
var a_FOO = {_id: 2, a: "FOO", geo: {type: "Point", coordinates: [0, 0]}};
var a_foo = {_id: 3, a: "foo", geo: {type: "Point", coordinates: [0, 0]}};
assert.commandWorked(collCaseInsensitive.insert(a_1));
assert.commandWorked(collCaseInsensitive.insert(a_100));
assert.commandWorked(collCaseInsensitive.insert(a_FOO));
assert.commandWorked(collCaseInsensitive.insert(a_foo));

// Aggregate.

// Test an aggregate command on strings with a non-simple collation inherited from the
// collection default. This should be scatter-gather.
assert.eq(2, collCaseInsensitive.aggregate([{$match: {a: "foo"}}]).itcount());
explain = collCaseInsensitive.explain().aggregate([{$match: {a: "foo"}}]);
assert.commandWorked(explain);
assert.eq(3, Object.keys(explain.shards).length);

// Test an aggregate command with a simple collation. This should be single-shard.
assert.eq(1,
          collCaseInsensitive.aggregate([{$match: {a: "foo"}}], {collation: {locale: "simple"}})
              .itcount());
explain = collCaseInsensitive.explain().aggregate([{$match: {a: "foo"}}],
                                                  {collation: {locale: "simple"}});
assert.commandWorked(explain);
assert.eq(1, Object.keys(explain.shards).length);

// Test an aggregate command on numbers with a non-simple collation inherited from the
// collection default. This should be single-shard.
assert.eq(1, collCaseInsensitive.aggregate([{$match: {a: 100}}]).itcount());
explain = collCaseInsensitive.explain().aggregate([{$match: {a: 100}}]);
assert.commandWorked(explain);
assert.eq(1, Object.keys(explain.shards).length);

// Aggregate with $geoNear.
const geoJSONPoint = {
    type: "Point",
    coordinates: [0, 0]
};

// Test $geoNear with a query on strings with a non-simple collation inherited from the
// collection default. This should scatter-gather.
const geoNearStageStringQuery = [{
    $geoNear: {
        near: geoJSONPoint,
        distanceField: "dist",
        spherical: true,
        query: {a: "foo"},
    }
}];
assert.eq(2, collCaseInsensitive.aggregate(geoNearStageStringQuery).itcount());
explain = collCaseInsensitive.explain().aggregate(geoNearStageStringQuery);
assert.commandWorked(explain);
assert.eq(3, Object.keys(explain.shards).length);

// Test $geoNear with a query on strings with a simple collation. This should be single-shard.
assert.eq(1,
          collCaseInsensitive.aggregate(geoNearStageStringQuery, {collation: {locale: "simple"}})
              .itcount());
explain = collCaseInsensitive.explain().aggregate(geoNearStageStringQuery,
                                                  {collation: {locale: "simple"}});
assert.commandWorked(explain);
assert.eq(1, Object.keys(explain.shards).length);

// Test a $geoNear with a query on numbers with a non-simple collation inherited from the
// collection default. This should be single-shard.
const geoNearStageNumericalQuery = [{
    $geoNear: {
        near: geoJSONPoint,
        distanceField: "dist",
        spherical: true,
        query: {a: 100},
    }
}];
assert.eq(1, collCaseInsensitive.aggregate(geoNearStageNumericalQuery).itcount());
explain = collCaseInsensitive.explain().aggregate(geoNearStageNumericalQuery);
assert.commandWorked(explain);
assert.eq(1, Object.keys(explain.shards).length);

// Count.

// Test a count command on strings with a non-simple collation inherited from the collection
// default. This should be scatter-gather.
assert.eq(2, collCaseInsensitive.find({a: "foo"}).count());
explain = collCaseInsensitive.explain().find({a: "foo"}).count();
assert.commandWorked(explain);
assert.eq(3, explain.queryPlanner.winningPlan.shards.length);

// Test a count command with a simple collation. This should be single-shard.
assert.eq(1, collCaseInsensitive.find({a: "foo"}).collation({locale: "simple"}).count());
explain = collCaseInsensitive.explain().find({a: "foo"}).collation({locale: "simple"}).count();
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Test a find command on numbers with a non-simple collation inheritied from the collection
// default. This should be single-shard.
assert.eq(1, collCaseInsensitive.find({a: 100}).count());
explain = collCaseInsensitive.explain().find({a: 100}).count();
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Distinct.

// Test a distinct command on strings with a non-simple collation inherited from the collection
// default. This should be scatter-gather.
assert.eq(2, collCaseInsensitive.distinct("_id", {a: "foo"}).length);
explain = collCaseInsensitive.explain().distinct("_id", {a: "foo"});
assert.commandWorked(explain);
assert.eq(3, explain.queryPlanner.winningPlan.shards.length);

// Test that deduping respects the collation inherited from the collection default.
assert.eq(1, collCaseInsensitive.distinct("a", {a: "foo"}).length);

// Test a distinct command with a simple collation. This should be single-shard.
assert.eq(1,
          collCaseInsensitive.distinct("_id", {a: "foo"}, {collation: {locale: "simple"}}).length);
explain =
    collCaseInsensitive.explain().distinct("_id", {a: "foo"}, {collation: {locale: "simple"}});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Test a distinct command on numbers with a non-simple collation inherited from the collection
// default. This should be single-shard.
assert.eq(1, collCaseInsensitive.distinct("_id", {a: 100}).length);
explain = collCaseInsensitive.explain().distinct("_id", {a: 100});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Find.

// Test a find command on strings with a non-simple collation inherited from the collection
// default. This should be scatter-gather.
assert.eq(2, collCaseInsensitive.find({a: "foo"}).itcount());
explain = collCaseInsensitive.find({a: "foo"}).explain();
assert.commandWorked(explain);
assert.eq(3, explain.queryPlanner.winningPlan.shards.length);

// Test a find command with a simple collation. This should be single-shard.
assert.eq(1, collCaseInsensitive.find({a: "foo"}).collation({locale: "simple"}).itcount());
explain = collCaseInsensitive.find({a: "foo"}).collation({locale: "simple"}).explain();
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Test a find command on numbers with a non-simple collation inherited from the collection
// default. This should be single-shard.
assert.eq(1, collCaseInsensitive.find({a: 100}).itcount());
explain = collCaseInsensitive.find({a: 100}).explain();
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// FindAndModify.

// Sharded findAndModify that does not target a single shard can now be executed with a two phase
// write protocol that will target at most 1 matching document.
if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) {
    let res = collCaseInsensitive.findAndModify({query: {a: "foo"}, update: {$set: {b: 1}}});
    assert(res.a === "foo" || res.a === "FOO");
    explain =
        collCaseInsensitive.explain().findAndModify({query: {a: "foo"}, update: {$set: {b: 1}}});
    assert.commandWorked(explain);
    assert.eq(1, explain.queryPlanner.winningPlan.shards.length);
} else {
    // Sharded findAndModify on strings with non-simple collation inherited from the collection
    // default should fail, because findAndModify must target a single shard.
    assert.throws(function() {
        collCaseInsensitive.findAndModify({query: {a: "foo"}, update: {$set: {b: 1}}});
    });
    assert.throws(function() {
        collCaseInsensitive.explain().findAndModify({query: {a: "foo"}, update: {$set: {b: 1}}});
    });
}

// Sharded findAndModify on strings with simple collation should succeed. This should be
// single-shard.
assert.eq(
    "foo",
    collCaseInsensitive
        .findAndModify({query: {a: "foo"}, update: {$set: {b: 1}}, collation: {locale: "simple"}})
        .a);
explain = collCaseInsensitive.explain().findAndModify(
    {query: {a: "foo"}, update: {$set: {b: 1}}, collation: {locale: "simple"}});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Sharded findAndModify on numbers with non-simple collation inherited from collection default
// should succeed. This should be single-shard.
assert.eq(100, collCaseInsensitive.findAndModify({query: {a: 100}, update: {$set: {b: 1}}}).a);
explain = collCaseInsensitive.explain().findAndModify({query: {a: 100}, update: {$set: {b: 1}}});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// MapReduce.

// Test that the filter on mapReduce respects the non-simple collation inherited from the
// collection default.
assert.eq(2,
          assert
              .commandWorked(collCaseInsensitive.mapReduce(
                  function() {
                      emit(this._id, 1);
                  },
                  function(key, values) {
                      return Array.sum(values);
                  },
                  {out: {inline: 1}, query: {a: "foo"}}))
              .results.length);

// Test that mapReduce respects the non-simple default collation for the emitted keys. In this
// case, the emitted keys "foo" and "FOO" should be considered equal.
assert.eq(1,
          assert
              .commandWorked(collCaseInsensitive.mapReduce(
                  function() {
                      emit(this.a, 1);
                  },
                  function(key, values) {
                      return Array.sum(values);
                  },
                  {out: {inline: 1}, query: {a: "foo"}}))
              .results.length);

// Test that the filter on mapReduce respects the simple collation if specified.
assert.eq(1,
          assert
              .commandWorked(collCaseInsensitive.mapReduce(
                  function() {
                      emit(this.a, 1);
                  },
                  function(key, values) {
                      return Array.sum(values);
                  },
                  {out: {inline: 1}, query: {a: "foo"}, collation: {locale: "simple"}}))
              .results.length);

// Test that mapReduce respects the user-specified simple collation for the emitted keys.
assert.eq(2,
          assert
              .commandWorked(collCaseInsensitive.mapReduce(
                  function() {
                      emit(this.a, 1);
                  },
                  function(key, values) {
                      return Array.sum(values);
                  },
                  {out: {inline: 1}, query: {a: {$type: "string"}}, collation: {locale: "simple"}}))
              .results.length);

// Remove.

// Test a remove command on strings with non-simple collation inherited from collection default.
// This should be scatter-gather.
writeRes = collCaseInsensitive.remove({a: "foo"});
assert.commandWorked(writeRes);
assert.eq(2, writeRes.nRemoved);
explain = collCaseInsensitive.explain().remove({a: "foo"});
assert.commandWorked(explain);
assert.eq(3, explain.queryPlanner.winningPlan.shards.length);
assert.commandWorked(collCaseInsensitive.insert(a_FOO));
assert.commandWorked(collCaseInsensitive.insert(a_foo));

// Test a remove command on strings with simple collation. This should be single-shard.
writeRes = collCaseInsensitive.remove({a: "foo"}, {collation: {locale: "simple"}});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nRemoved);
explain = collCaseInsensitive.explain().remove({a: "foo"}, {collation: {locale: "simple"}});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);
assert.commandWorked(collCaseInsensitive.insert(a_foo));

// Test a remove command on numbers with non-simple collation inherited from collection default.
// This should be single-shard.
writeRes = collCaseInsensitive.remove({a: 100});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nRemoved);
explain = collCaseInsensitive.explain().remove({a: 100});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);
assert.commandWorked(collCaseInsensitive.insert(a_100));

// Sharded deleteOne that does not target a single shard can now be executed with a two phase
// write protocol that will target at most 1 matching document.
if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) {
    let beforeNumDocsMatch =
        collCaseInsensitive.find({a: "foo"}).collation(caseInsensitive).count();
    writeRes = assert.commandWorked(collCaseInsensitive.remove({a: "foo"}, {justOne: true}));
    assert.eq(1, writeRes.nRemoved);
    let afterNumDocsMatch = collCaseInsensitive.find({a: "foo"}).collation(caseInsensitive).count();
    assert.eq(beforeNumDocsMatch - 1, afterNumDocsMatch);
    explain = collCaseInsensitive.explain().remove({a: "foo"}, {justOne: true});
    assert.commandWorked(explain);
    assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

    // Re-insert documents for later test cases.
    collCaseInsensitive.insert(a_foo);
    collCaseInsensitive.insert(a_FOO);
} else {
    // A single remove (justOne: true) must be single-shard or an exact-ID query. A query is
    // exact-ID if it contains an equality on _id and either has the collection default collation or
    // _id is not a string/object/array.

    // Single remove on string shard key with non-simple collation inherited from collection default
    // should fail, because it is not single-shard.
    assert.writeError(collCaseInsensitive.remove({a: "foo"}, {justOne: true}));

    // Single remove on string shard key with simple collation should succeed, because it is
    // single-shard.
    writeRes =
        collCaseInsensitive.remove({a: "foo"}, {justOne: true, collation: {locale: "simple"}});
    assert.commandWorked(writeRes);
    assert.eq(1, writeRes.nRemoved);
    explain = collCaseInsensitive.explain().remove({a: "foo"},
                                                   {justOne: true, collation: {locale: "simple"}});
    assert.commandWorked(explain);
    assert.eq(1, explain.queryPlanner.winningPlan.shards.length);
    assert.commandWorked(collCaseInsensitive.insert(a_foo));
}

// Single remove on number shard key with non-simple collation inherited from collection default
// should succeed, because it is single-shard.
writeRes = collCaseInsensitive.remove({a: 100}, {justOne: true});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nRemoved);
explain = collCaseInsensitive.explain().remove({a: 100}, {justOne: true});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);
assert.commandWorked(collCaseInsensitive.insert(a_100));

// Sharded deleteOne that does not target a single shard can now be executed with a two phase
// write protocol that will target at most 1 matching document.
if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) {
    // Isolate the insert-remove within the scope of this feature flag check to prevent side effects
    // on the collection.
    assert.commandWorked(collCaseInsensitive.insert({_id: "foo_scoped", a: "bar"}));
    writeRes = collCaseInsensitive.remove({_id: "foo_scoped"},
                                          {justOne: true, collation: {locale: "simple"}});
    assert.commandWorked(writeRes);
    assert.eq(1, writeRes.nRemoved);
} else {
    // Single remove on string _id with non-collection-default collation should fail, because it is
    // not an exact-ID query.
    assert.writeError(
        collCaseInsensitive.remove({_id: "foo"}, {justOne: true, collation: {locale: "simple"}}));
}

// Single remove on string _id with collection-default collation should succeed, because it is
// an exact-ID query.
assert.commandWorked(collCaseInsensitive.insert({_id: "foo", a: "bar"}));
writeRes = collCaseInsensitive.remove({_id: "foo"}, {justOne: true});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nRemoved);

// Single remove on string _id with collection-default collation explicitly given should
// succeed, because it is an exact-ID query.
assert.commandWorked(collCaseInsensitive.insert({_id: "foo", a: "bar"}));
writeRes = collCaseInsensitive.remove({_id: "foo"}, {justOne: true, collation: caseInsensitive});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nRemoved);

// Single remove on number _id with non-collection-default collation should succeed, because it
// is an exact-ID query.
writeRes =
    collCaseInsensitive.remove({_id: a_100._id}, {justOne: true, collation: {locale: "simple"}});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nRemoved);
assert.commandWorked(collCaseInsensitive.insert(a_100));

// Update.

// Test an update command on strings with non-simple collation inherited from collection
// default. This should be scatter-gather.
writeRes = collCaseInsensitive.update({a: "foo"}, {$set: {b: 1}}, {multi: true});
assert.commandWorked(writeRes);
assert.eq(2, writeRes.nMatched);
explain = collCaseInsensitive.explain().update({a: "foo"}, {$set: {b: 1}}, {multi: true});
assert.commandWorked(explain);
assert.eq(3, explain.queryPlanner.winningPlan.shards.length);

// Test an update command on strings with simple collation. This should be single-shard.
writeRes = collCaseInsensitive.update(
    {a: "foo"}, {$set: {b: 1}}, {multi: true, collation: {locale: "simple"}});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nMatched);
explain = collCaseInsensitive.explain().update(
    {a: "foo"}, {$set: {b: 1}}, {multi: true, collation: {locale: "simple"}});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Test an update command on numbers with non-simple collation inherited from collection
// default. This should be single-shard.
writeRes = collCaseInsensitive.update({a: 100}, {$set: {b: 1}}, {multi: true});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nMatched);
explain = collCaseInsensitive.explain().update({a: 100}, {$set: {b: 1}}, {multi: true});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Sharded updateOne that does not target a single shard can now be executed with a two phase
// write protocol that will target at most 1 matching document.
if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) {
    writeRes = assert.commandWorked(collCaseInsensitive.update({a: "foo"}, {$set: {b: 1}}));
    assert.eq(1, writeRes.nMatched);
    explain = collCaseInsensitive.explain().update({a: "foo"}, {$set: {b: 1}});
    assert.commandWorked(explain);
    assert.eq(1, explain.queryPlanner.winningPlan.shards.length);
} else {
    // A single (non-multi) update must be single-shard or an exact-ID query. A query is exact-ID if
    // it
    // contains an equality on _id and either has the collection default collation or _id is not a
    // string/object/array.

    // Single update on string shard key with non-simple collation inherited from collection default
    // should fail, because it is not single-shard.
    assert.writeError(collCaseInsensitive.update({a: "foo"}, {$set: {b: 1}}));
}

// Single update on string shard key with simple collation should succeed, because it is
// single-shard.
writeRes = collCaseInsensitive.update({a: "foo"}, {$set: {b: 1}}, {collation: {locale: "simple"}});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nMatched);
explain = collCaseInsensitive.explain().update(
    {a: "foo"}, {$set: {b: 1}}, {collation: {locale: "simple"}});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Single update on number shard key with non-simple collation inherited from collation default
// should succeed, because it is single-shard.
writeRes = collCaseInsensitive.update({a: 100}, {$set: {b: 1}});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nMatched);
explain = collCaseInsensitive.explain().update({a: 100}, {$set: {b: 1}});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Sharded updateOne that does not target a single shard can now be executed with a two phase
// write protocol that will target at most 1 matching document.
if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) {
    assert.commandWorked(
        collCaseInsensitive.update({_id: "foo"}, {$set: {b: 1}}, {collation: {locale: "simple"}}));
} else {
    // Single update on string _id with non-collection-default collation should fail, because it is
    // not an exact-ID query.
    assert.writeError(
        collCaseInsensitive.update({_id: "foo"}, {$set: {b: 1}}, {collation: {locale: "simple"}}));
}

// Single update on string _id with collection-default collation should succeed, because it is
// an exact-ID query.
assert.commandWorked(collCaseInsensitive.insert({_id: "foo", a: "bar"}));
writeRes = collCaseInsensitive.update({_id: "foo"}, {$set: {b: 1}});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nMatched);
assert.commandWorked(collCaseInsensitive.remove({_id: "foo"}, {justOne: true}));

// Single update on string _id with collection-default collation explicitly given should
// succeed, because it is an exact-ID query.
assert.commandWorked(collCaseInsensitive.insert({_id: "foo", a: "bar"}));
writeRes = collCaseInsensitive.update({_id: "foo"}, {$set: {b: 1}}, {collation: caseInsensitive});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nMatched);
assert.commandWorked(collCaseInsensitive.remove({_id: "foo"}, {justOne: true}));

// Single update on number _id with non-collection-default collation inherited from collection
// default should succeed, because it is an exact-ID query.
writeRes =
    collCaseInsensitive.update({_id: a_foo._id}, {$set: {b: 1}}, {collation: {locale: "simple"}});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nMatched);

// Sharded upsert that does not target a single shard can now be executed with a two phase
// write protocol that will target at most 1 matching document.
if (WriteWithoutShardKeyTestUtil.isWriteWithoutShardKeyFeatureEnabled(testDB)) {
    writeRes =
        collCaseInsensitive.update({a: "filter"}, {$set: {b: 1}}, {multi: false, upsert: true});
    assert.commandWorked(writeRes);
    assert.eq(1, writeRes.nUpserted);
} else {
    // Upsert must always be single-shard.

    // Upsert on strings with non-simple collation inherited from collection default should fail,
    // because it is not single-shard.
    assert.writeError(
        collCaseInsensitive.update({a: "foo"}, {$set: {b: 1}}, {multi: true, upsert: true}));
}

// Upsert on strings with simple collation should succeed, because it is single-shard.
writeRes = collCaseInsensitive.update(
    {a: "foo"}, {$set: {b: 1}}, {multi: true, upsert: true, collation: {locale: "simple"}});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nMatched);
explain = collCaseInsensitive.explain().update(
    {a: "foo"}, {$set: {b: 1}}, {multi: true, upsert: true, collation: {locale: "simple"}});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

// Upsert on numbers with non-simple collation inherited from collection default should succeed,
// because it is single-shard.
writeRes = collCaseInsensitive.update({a: 100}, {$set: {b: 1}}, {multi: true, upsert: true});
assert.commandWorked(writeRes);
assert.eq(1, writeRes.nMatched);
explain =
    collCaseInsensitive.explain().update({a: 100}, {$set: {b: 1}}, {multi: true, upsert: true});
assert.commandWorked(explain);
assert.eq(1, explain.queryPlanner.winningPlan.shards.length);

st.stop();
})();