summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/merge/mode_pipeline_insert.js
blob: e37f5467b813c927df4521c11e9b604a32c75ee3 (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
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
// Tests the behaviour of the $merge stage with whenMatched=[<pipeline>] and whenNotMatched=insert.
//
// Cannot implicitly shard accessed collections because a collection can be implictly created and
// exists when none is expected.
// @tags: [assumes_no_implicit_collection_creation_after_drop]
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");  // For assertArrayEq.
load("jstests/libs/fixture_helpers.js");      // For FixtureHelpers.isMongos.

// A helper function to create a pipeline with a $merge stage using a custom 'updatePipeline'
// for the whenMatched mode. If 'initialStages' array is specified, the $merge stage will be
// appended to this array and the result returned to the caller, otherwise an array with a
// single $merge stage is returned. An output collection for the $merge stage is specified
// in the 'target', and the $merge stage 'on' fields in the 'on' parameter. The 'letVars'
// parameter describes the 'let' argument of the $merge stage and holds variables that can be
// referenced in the pipeline.
function makeMergePipeline(
    {target = "", initialStages = [], updatePipeline = [], on = "_id", letVars = undefined} = {}) {
    const baseObj = letVars !== undefined ? {let : letVars} : {};
    return initialStages.concat([{
        $merge: Object.assign(
            baseObj, {into: target, on: on, whenMatched: updatePipeline, whenNotMatched: "insert"})
    }]);
}

const source = db[`${jsTest.name()}_source`];
source.drop();
const target = db[`${jsTest.name()}_target`];
target.drop();

(function testMergeIntoNonExistentCollection() {
    assert.commandWorked(source.insert({_id: 1, a: 1, b: "a"}));
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline(
                            {target: target.getName(), updatePipeline: [{$addFields: {x: 1}}]})));
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [
            {_id: 1, a: 1, b: "a"},
        ]
    });
})();

// Test $merge inserts the original source document into an existing target collection if no
// matching document is found.
(function testMergeInsertsDocumentIfMatchNotFound() {
    assert.commandWorked(target.deleteMany({}));
    assert.doesNotThrow(
        () => source.aggregate(makeMergePipeline(
            {target: target.getName(), updatePipeline: [{$addFields: {x: 1, y: 2}}]})));
    assertArrayEq({actual: target.find().toArray(), expected: [{_id: 1, a: 1, b: "a"}]});
})();

// Test $merge updates an existing document in the target collection by applying a
// pipeline-style update.
(function testMergeUpdatesDocumentIfMatchFound() {
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        target: target.getName(),
        updatePipeline:
            [{$project: {x: {$add: ["$a", 1]}, y: {$sum: ["$y", 2]}, z: {$add: ["$y", 2]}}}]
    })));
    assertArrayEq({actual: target.find().toArray(), expected: [{_id: 1, x: 2, y: 2, z: null}]});
})();

// Test $merge with various pipeline stages which are currently supported by the pipeline-style
// update.
(function testMergeWithSupportedUpdatePipelineStages() {
    assert(source.drop());
    assert(target.drop());

    assert.commandWorked(source.insert([{_id: 1, a: 1}, {_id: 2, a: 2}]));
    assert.commandWorked(target.insert({_id: 1, b: 1}));

    // Test $addFields stage.
    assert.doesNotThrow(
        () => source.aggregate(makeMergePipeline(
            {target: target.getName(), updatePipeline: [{$addFields: {x: {$add: ["$b", 1]}}}]})));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, b: 1, x: 2}, {_id: 2, a: 2}]});

    // Test $project stage.
    assert(target.drop());
    assert.commandWorked(target.insert({_id: 1, b: 1}));
    assert.doesNotThrow(
        () => source.aggregate(makeMergePipeline(
            {target: target.getName(), updatePipeline: [{$project: {x: {$add: ["$b", 1]}}}]})));
    assertArrayEq({actual: target.find().toArray(), expected: [{_id: 1, x: 2}, {_id: 2, a: 2}]});

    // Test $replaceWith stage.
    assert(target.drop());
    assert.commandWorked(
        target.insert([{_id: 1, b: 1, c: {x: {y: 1}}}, {_id: 2, b: 2, c: {x: {y: 2}}}]));
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline(
                            {target: target.getName(), updatePipeline: [{$replaceWith: "$c"}]})));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, x: {y: 1}}, {_id: 2, x: {y: 2}}]});

    // Test $replaceRoot stage.
    assert(target.drop());
    assert.commandWorked(
        target.insert([{_id: 1, b: 1, c: {x: {y: 1}}}, {_id: 2, b: 2, c: {x: {y: 2}}}]));
    assert.doesNotThrow(
        () => source.aggregate(makeMergePipeline(
            {target: target.getName(), updatePipeline: [{$replaceRoot: {newRoot: "$c"}}]})));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, x: {y: 1}}, {_id: 2, x: {y: 2}}]});
})();

// Test $merge inserts a new document into the target collection if no matching document is
// found by applying a pipeline-style update with upsert=true and upsertSupplied=true.
(function testMergeInsertDocumentIfMatchNotFound() {
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert({_id: 1, a: 1}));
    assert.commandWorked(target.insert({_id: 2, a: 2}));
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline(
                            {target: target.getName(), updatePipeline: [{$addFields: {x: 1}}]})));
    assertArrayEq({actual: target.find().toArray(), expected: [{_id: 1, a: 1}, {_id: 2, a: 2}]});
})();

// Test $merge doesn't modify the target collection if a document has been removed from the
// source collection.
(function testMergeDoesNotUpdateDeletedDocument() {
    assert.commandWorked(source.deleteOne({_id: 1}));
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        target: target.getName(),
        updatePipeline: [{$project: {x: {$add: ["$x", 1]}, a: 1}}]
    })));
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [
            {_id: 1, a: 1},
            {_id: 2, a: 2},
        ]
    });
})();

// Test $merge fails if a unique index constraint in the target collection is violated.
(function testMergeFailsIfTargetUniqueKeyIsViolated() {
    if (FixtureHelpers.isSharded(source)) {
        // Skip this test if the collection sharded, because an implicitly created sharded
        // key of {_id: 1} will not be covered by a unique index created in this test, which
        // is not allowed.
        return;
    }

    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert({_id: 4, a: 2}));
    assert.commandWorked(target.insert([{_id: 1, x: 1}, {_id: 2, a: 2}]));
    assert.commandWorked(target.createIndex({a: 1}, {unique: true}));
    const error =
        assert.throws(() => source.aggregate(makeMergePipeline(
                          {target: target.getName(), updatePipeline: [{$project: {x: 1, a: 1}}]})));
    assert.commandFailedWithCode(error, ErrorCodes.DuplicateKey);
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [
            {_id: 1, x: 1},
            {_id: 2, a: 2},
        ]
    });
    assert.commandWorked(target.dropIndex({a: 1}));
})();

// Test $merge fails if it cannot find an index to verify that the 'on' fields will be unique.
(function testMergeFailsIfOnFieldCannotBeVerifiedForUniquness() {
    // The 'on' fields contains a single document field.
    let error = assert.throws(() => source.aggregate(makeMergePipeline({
        target: target.getName(),
        on: "nonexistent",
        updatePipeline: [{$project: {x: 1, a: 1}}]
    })));
    assert.commandFailedWithCode(error, [51190, 51183]);

    // The 'on' fields contains multiple document fields.
    error = assert.throws(() => source.aggregate(makeMergePipeline({
        target: target.getName(),
        on: ["nonexistent1", "nonexistent2"],
        updatePipeline: [{$project: {x: 1, a: 1}}]
    })));
    assert.commandFailedWithCode(error, [51190, 51183]);
})();

// Test $merge with an explicit 'on' field over a single or multiple document fields which
// differ from the _id field.
(function testMergeWithOnFields() {
    if (FixtureHelpers.isSharded(source)) {
        // Skip this test if the collection is sharded, because an implicitly created sharded
        // key of {_id: 1} will not be covered by a unique index created in this test, which
        // is not allowed.
        return;
    }

    // The 'on' fields contains a single document field.
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.createIndex({a: 1}, {unique: true}));
    assert.commandWorked(target.createIndex({a: 1}, {unique: true}));
    assert.commandWorked(source.insert([{_id: 1, a: 1}, {_id: 2, a: 2}, {_id: 3, a: 30}]));
    assert.commandWorked(
        target.insert([{_id: 1, a: 1, b: 1}, {_id: 4, a: 30, b: 2}, {_id: 5, a: 40, b: 3}]));
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        initialStages: [{$project: {_id: 0}}],
        target: target.getName(),
        on: "a",
        updatePipeline: [{$addFields: {z: 1}}]
    })));
    assertArrayEq({
        actual: target.find({}, {_id: 0}).toArray(),
        expected: [{a: 1, b: 1, z: 1}, {a: 2}, {a: 30, b: 2, z: 1}, {a: 40, b: 3}]
    });

    // The 'on' fields contains multiple document fields.
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.createIndex({a: 1, b: 1}, {unique: true}));
    assert.commandWorked(target.createIndex({a: 1, b: 1}, {unique: true}));
    assert.commandWorked(
        source.insert([{_id: 1, a: 1, b: 1}, {_id: 2, a: 2, b: 4}, {_id: 3, a: 30, b: 2}]));
    assert.commandWorked(
        target.insert([{_id: 1, a: 1, b: 1}, {_id: 4, a: 30, b: 2}, {_id: 5, a: 40, b: 3}]));
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        initialStages: [{$project: {_id: 0}}],
        target: target.getName(),
        on: ["a", "b"],
        updatePipeline: [{$addFields: {z: 1}}]
    })));
    assertArrayEq({
        actual: target.find({}, {_id: 0}).toArray(),
        expected: [{a: 1, b: 1, z: 1}, {a: 2, b: 4}, {a: 30, b: 2, z: 1}, {a: 40, b: 3}]
    });
    assert.commandWorked(source.dropIndex({a: 1, b: 1}));
    assert.commandWorked(target.dropIndex({a: 1, b: 1}));
})();

// Test $merge with a dotted path in the 'on' field.
(function testMergeWithDottedOnField() {
    if (FixtureHelpers.isSharded(source)) {
        // Skip this test if the collection sharded, because an implicitly created sharded
        // key of {_id: 1} will not be covered by a unique index created in this test, which
        // is not allowed.
        return;
    }

    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.createIndex({"a.b": 1}, {unique: true}));
    assert.commandWorked(target.createIndex({"a.b": 1}, {unique: true}));
    assert.commandWorked(source.insert([
        {_id: 1, a: {b: "b"}, c: "x"},
        {_id: 2, a: {b: "c"}, c: "y"},
        {_id: 3, a: {b: 30}, b: "c"}
    ]));
    assert.commandWorked(target.insert({_id: 2, a: {b: "c"}, c: "y"}));
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        initialStages: [{$project: {_id: 0}}],
        target: target.getName(),
        on: "a.b",
        updatePipeline: [{$addFields: {z: 1}}]
    })));
    assertArrayEq({
        actual: target.find({}, {_id: 0}).toArray(),
        expected: [{a: {b: "b"}, c: "x"}, {a: {b: "c"}, c: "y", z: 1}, {a: {b: 30}, b: "c"}]
    });
})();

// Test $merge fails if the value of the 'on' field in a document is invalid, e.g. missing,
// null or an array.
(function testMergeFailsIfOnFieldIsInvalid() {
    if (FixtureHelpers.isSharded(source)) {
        // Skip this test if the collection sharded, because an implicitly created sharded
        // key of {_id: 1} will not be covered by a unique index created in this test, which
        // is not allowed.
        return;
    }

    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.createIndex({"z": 1}, {unique: true}));
    assert.commandWorked(target.createIndex({"z": 1}, {unique: true}));

    const pipeline = makeMergePipeline({
        initialStages: [{$project: {_id: 0}}],
        target: target.getName(),
        on: "z",
        updatePipeline: [{$addFields: {z: 1}}]
    });

    // The 'on' field is missing.
    assert.commandWorked(source.insert({_id: 1}));
    let error = assert.throws(() => source.aggregate(pipeline));
    assert.commandFailedWithCode(error, 51132);

    // The 'on' field is null.
    assert.commandWorked(source.update({_id: 1}, {z: null}));
    error = assert.throws(() => source.aggregate(pipeline));
    assert.commandFailedWithCode(error, 51132);

    // The 'on' field is an array.
    assert.commandWorked(source.update({_id: 1}, {z: [1, 2]}));
    error = assert.throws(() => source.aggregate(pipeline));
    assert.commandFailedWithCode(error, 51185);
})();

// Test $merge when the _id field is removed from the aggregate projection but is used in the
// $merge's 'on' field. When the _id is missing, the $merge stage will create a new ObjectId in
// its place before performing the insert or update.
(function testMergeWhenDocIdIsRemovedFromProjection() {
    let pipeline = makeMergePipeline({
        initialStages: [{$project: {_id: 0}}],
        target: target.getName(),
        updatePipeline: [{$addFields: {z: 1}}]
    });

    // The _id is a single 'on' field (a default one).
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert([{_id: 1, a: 1, b: "a"}, {_id: 2, a: 2, b: "b"}]));
    assert.commandWorked(target.insert({_id: 1, b: "c"}));
    assert.doesNotThrow(() => source.aggregate(pipeline));
    assertArrayEq({
        actual: target.find({}, {_id: 0}).toArray(),
        // There is a matching document in the target with {_id: 1}, but since we cannot match
        // it (no _id in projection), we insert the two documents from the source collection.
        expected: [{b: "c"}, {a: 1, b: "a"}, {a: 2, b: "b"}]
    });

    pipeline = makeMergePipeline({
        initialStages: [{$project: {_id: 0}}],
        on: ["_id", "a"],
        target: target.getName(),
        updatePipeline: [{$addFields: {z: 1}}]
    });

    // The _id is part of the compound 'on' field.
    assert(target.drop());
    assert.commandWorked(target.insert({_id: 1, b: "c"}));
    assert.commandWorked(source.createIndex({_id: 1, a: -1}, {unique: true}));
    assert.commandWorked(target.createIndex({_id: 1, a: -1}, {unique: true}));
    assert.doesNotThrow(() => source.aggregate(pipeline));
    assertArrayEq({
        actual: target.find({}, {_id: 0}).toArray(),
        expected: [{b: "c"}, {a: 1, b: "a"}, {a: 2, b: "b"}]
    });
    assert.commandWorked(source.dropIndex({_id: 1, a: -1}));
    assert.commandWorked(target.dropIndex({_id: 1, a: -1}));
})();

// Test $merge preserves indexes and options of the existing target collection.
(function testMergePresrvesIndexesAndOptions() {
    const validator = {z: {$gt: 0}};
    assert(target.drop());
    assert.commandWorked(db.createCollection(target.getName(), {validator: validator}));
    assert.commandWorked(target.createIndex({a: 1}));
    assert.commandWorked(target.insert([{_id: 1, z: 5}, {_id: 2, z: 5}]));
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline(
                            {target: target.getName(), updatePipeline: [{$addFields: {z: 1}}]})));
    assertArrayEq({actual: target.find().toArray(), expected: [{_id: 1, z: 1}, {_id: 2, z: 1}]});
    assert.eq(2, target.getIndexes().length);

    const listColl = db.runCommand({listCollections: 1, filter: {name: target.getName()}});
    assert.commandWorked(listColl);
    assert.eq(validator, listColl.cursor.firstBatch[0].options["validator"]);
})();

// Test that $merge implicitly creates a new database when the target collection's db doesn't exist.
(function testMergeImplicitlyCreatesTargetDatabase() {
    assert(source.drop());
    assert.commandWorked(source.insert({_id: 1, a: 1, b: "a"}));

    const foreignDb = db.getSiblingDB(`${jsTest.name()}_foreign_db`);
    assert.commandWorked(foreignDb.dropDatabase());
    const foreignTarget = foreignDb[`${jsTest.name()}_target`];
    const foreignPipeline = makeMergePipeline({
        target: {db: foreignDb.getName(), coll: foreignTarget.getName()},
        updatePipeline: [{$addFields: {z: 1}}]
    });

    if (!FixtureHelpers.isMongos(db)) {
        assert.doesNotThrow(() => source.aggregate(foreignPipeline));
        assertArrayEq({actual: foreignTarget.find().toArray(), expected: [{_id: 1, a: 1, b: "a"}]});
    } else {
        // Implicit database creation is prohibited in a cluster.
        const error = assert.throws(() => source.aggregate(foreignPipeline));
        assert.commandFailedWithCode(error, ErrorCodes.NamespaceNotFound);

        // Force creation of the database and collection, then fall through the test below.
        assert.commandWorked(foreignTarget.insert({_id: 1, a: 1, b: "a"}));
    }

    assert.doesNotThrow(() => source.aggregate(foreignPipeline));
    assertArrayEq(
        {actual: foreignTarget.find().toArray(), expected: [{_id: 1, a: 1, b: "a", z: 1}]});
    assert.commandWorked(foreignDb.dropDatabase());
})();

// Test that $merge can reference the default 'let' variable 'new' which holds the entire
// document from the source collection.
(function testMergeWithDefaultLetVariable() {
    assert(source.drop());
    assert(target.drop());

    assert.commandWorked(source.insert([{_id: 1, a: 1, b: 1}, {_id: 2, a: 2, b: 2}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        target: target.getName(),
        updatePipeline: [{$set: {x: {$add: ["$$new.a", "$$new.b"]}}}]
    })));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, c: 1, x: 2}, {_id: 2, a: 2, b: 2}]});
})();

// Test that the default 'let' variable 'new' is always available even when the 'let' argument to
// the $merge stage is specified explicitly.
(function testMergeCannotUseDefaultLetVariableIfLetIsSpecified() {
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert([{_id: 1, a: 1, b: 1}, {_id: 2, a: 2, b: 2}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: {foo: "bar"},
        target: target.getName(),
        updatePipeline: [{$project: {x: "$$new.a", y: "$$new.b"}}]
    })));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, x: 1, y: 1}, {_id: 2, a: 2, b: 2}]});
})();

// Test that $merge can accept an empty object holding no variables and the default 'new'
// variable is still available.
(function testMergeWithEmptyLetVariables() {
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert([{_id: 1, a: 1, b: 1}, {_id: 2, a: 2, b: 2}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    // Can use an empty object.
    assert.doesNotThrow(
        () => source.aggregate(makeMergePipeline(
            {letVars: {}, target: target.getName(), updatePipeline: [{$set: {x: "foo"}}]})));
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [{_id: 1, c: 1, x: "foo"}, {_id: 2, a: 2, b: 2}]
    });

    // No default variable 'new' is available.
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: {},
        target: target.getName(),
        updatePipeline: [{$project: {x: "$$new.a", y: "$$new.b"}}]
    })));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, x: 1, y: 1}, {_id: 2, x: 2, y: 2}]});
})();

// Test that $merge will reject a 'let' specification which attempts to redefine 'new'.
(function testMergeRejectsLetVariablesWhichRedefineNew() {
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert([{_id: 1, a: 1, b: 1}, {_id: 2, a: 2, b: 2}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    // Cannot override 'new' with an arbitrary value.
    const error = assert.throws(() => source.aggregate(makeMergePipeline({
        letVars: {new: "$a"},
        target: target.getName(),
        updatePipeline: [{$set: {x: "foo"}}]
    })));
    assert.commandFailedWithCode(error, 51273);

    // If the user's 'let' explicitly sets 'new' to "$$ROOT", we allow it.
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: {new: "$$ROOT"},
        target: target.getName(),
        updatePipeline: [{$project: {x: "$$new.a", y: "$$new.b"}}]
    })));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, x: 1, y: 1}, {_id: 2, a: 2, b: 2}]});
})();

// Test that $merge can accept a null value as the 'let' argument and the default variable 'new'
// can be used.
// Note that this is not a desirable behaviour but rather a limitation in the IDL parser which
// cannot differentiate between an optional field specified explicitly as 'null', or not
// specified at all. In both cases it will treat the field like it wasn't specified. So, this
// test ensures that we're aware of this limitation. Once the limitation is addressed in
// SERVER-41272, this test should be updated accordingly.
(function testMergeWithNullLetVariables() {
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert([{_id: 1, a: 1, b: 1}, {_id: 2, a: 2, b: 2}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    // Can use a null 'let' argument.
    assert.doesNotThrow(
        () => source.aggregate(makeMergePipeline(
            {letVars: null, target: target.getName(), updatePipeline: [{$set: {x: "foo"}}]})));
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [{_id: 1, c: 1, x: "foo"}, {_id: 2, a: 2, b: 2}]
    });

    // Can use the default 'new' variable.
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: null,
        target: target.getName(),
        updatePipeline: [{$project: {x: "$$new.a", y: "$$new.b"}}]
    })));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, x: 1, y: 1}, {_id: 2, x: 2, y: 2}]});
})();

// Test that constant values can be specified in the 'let' argument and referenced in the update
// pipeline.
(function testMergeWithConstantLetVariable() {
    // Non-array constants.
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert([{_id: 1, a: 1, b: 1}, {_id: 2, a: 2, b: 2}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: {a: 1, b: "foo", c: true},
        target: target.getName(),
        updatePipeline: [{$set: {x: "$$a", y: "$$b", z: "$$c"}}]
    })));
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [{_id: 1, c: 1, x: 1, y: "foo", z: true}, {_id: 2, a: 2, b: 2}]
    });

    // Constant array.
    assert(target.drop());
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: {a: [1, 2, 3]},
        target: target.getName(),
        updatePipeline: [{$set: {x: {$arrayElemAt: ["$$a", 1]}}}]
    })));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, c: 1, x: 2}, {_id: 2, a: 2, b: 2}]});
})();

// Test that variables referencing the fields in the source document can be specified in the
// 'let' argument and referenced in the update pipeline.
(function testMergeWithNonConstantLetVariables() {
    // Non-array fields.
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert([{_id: 1, a: 1, b: 1}, {_id: 2, a: 2, b: 2}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: {x: "$a", y: "$b"},
        target: target.getName(),
        updatePipeline: [{$set: {z: {$add: ["$$x", "$$y"]}}}]
    })));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, c: 1, z: 2}, {_id: 2, a: 2, b: 2}]});

    // Array field with expressions in the pipeline.
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert([{_id: 1, a: [1, 2, 3]}, {_id: 2, a: [4, 5, 6]}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: {x: "$a"},
        target: target.getName(),
        updatePipeline: [{$set: {z: {$arrayElemAt: ["$$x", 1]}}}]
    })));
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [{_id: 1, c: 1, z: 2}, {_id: 2, a: [4, 5, 6]}]
    });

    // Array field with expressions in the 'let' argument.
    assert(target.drop());
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: {x: {$arrayElemAt: ["$a", 2]}},
        target: target.getName(),
        updatePipeline: [{$set: {z: "$$x"}}]
    })));
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [{_id: 1, c: 1, z: 3}, {_id: 2, a: [4, 5, 6]}]
    });
})();

// Test that variables using the dotted path can be specified in the 'let' argument and
// referenced in the update pipeline.
(function testMergeWithDottedPathLetVariables() {
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(source.insert([{_id: 1, a: {b: {c: 2}}}, {_id: 2, a: {b: {c: 3}}}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        letVars: {x: "$a.b.c"},
        target: target.getName(),
        updatePipeline: [{$set: {z: {$pow: ["$$x", 2]}}}]
    })));
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [{_id: 1, c: 1, z: 4}, {_id: 2, a: {b: {c: 3}}}]
    });
})();

// Test that 'let' variables are referred to the computed document in the aggregation pipeline,
// not the original document in the source collection.
(function testMergeLetVariablesHoldsComputedValues() {
    // Test the default 'new' variable.
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(
        source.insert([{_id: 1, a: 1, b: 1}, {_id: 2, a: 1, b: 2}, {_id: 3, a: 2, b: 3}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    // In the $group stage the total field 'a' uses the same name as in the source collection
    // intentionally, to make sure that even when a referenced field is present in the source
    // collection under the same name, the actual value for the variable will be picked up from
    // the computed document.
    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        initialStages: [{$group: {_id: "$a", a: {$sum: "$b"}}}],
        target: target.getName(),
        updatePipeline: [{$set: {z: "$$new"}}]
    })));
    assertArrayEq({
        actual: target.find().toArray(),
        expected: [{_id: 1, c: 1, z: {_id: 1, a: 3}}, {_id: 2, a: 3}]
    });

    // Test custom 'let' variables.
    assert(source.drop());
    assert(target.drop());
    assert.commandWorked(
        source.insert([{_id: 1, a: 1, b: 5}, {_id: 2, a: 1, b: 2}, {_id: 3, a: 2, b: 3}]));
    assert.commandWorked(target.insert({_id: 1, c: 1}));

    assert.doesNotThrow(() => source.aggregate(makeMergePipeline({
        initialStages: [{$group: {_id: "$a", a: {$sum: "$b"}}}],
        letVars: {x: {$pow: ["$a", 2]}},
        target: target.getName(),
        updatePipeline: [{$set: {z: "$$x"}}]
    })));
    assertArrayEq(
        {actual: target.find().toArray(), expected: [{_id: 1, c: 1, z: 49}, {_id: 2, a: 3}]});
})();
}());