summaryrefslogtreecommitdiff
path: root/jstests/core/update_pipeline_shell_helpers.js
blob: 36f373f7811df7466f5387c62ecbf2361c7df767 (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
/**
 * Tests that each of the update shell helpers correctly validates pipeline-style update operations.
 *
 * This test is tagged as 'requires_find_command' to exclude it from the legacy passthroughs, since
 * pipeline syntax cannot be used for OP_UPDATE requests.
 *
 * @tags: [requires_find_command, requires_non_retryable_writes, assumes_write_concern_unchanged]
 */
(function() {
    "use strict";

    load("jstests/aggregation/extras/utils.js");  // For 'arrayEq'.

    // Make sure that the test collection is empty before starting the test.
    const testColl = db.update_pipeline_shell_helpers_test;
    testColl.drop();

    // Insert some test documents.
    assert.commandWorked(testColl.insert({_id: 1, a: 1, b: 2}));
    assert.commandWorked(testColl.insert({_id: 2, a: 2, b: 3}));

    // Test that each of the update shell helpers permits pipeline-style updates.
    assert.commandWorked(testColl.update({_id: 1}, [{$set: {update: true}}]));
    assert.commandWorked(testColl.update({}, [{$set: {updateMulti: true}}], {multi: true}));
    assert.commandWorked(testColl.updateOne({_id: 1}, [{$set: {updateOne: true}}]));
    assert.commandWorked(testColl.bulkWrite([
        {updateOne: {filter: {_id: 1}, update: [{$set: {bulkWriteUpdateOne: true}}]}},
        {updateMany: {filter: {}, update: [{$set: {bulkWriteUpdateMany: true}}]}}
    ]));

    // Test that each of the Bulk API update functions correctly handle pipeline syntax.
    const unorderedBulkOp = testColl.initializeUnorderedBulkOp();
    const orderedBulkOp = testColl.initializeOrderedBulkOp();

    unorderedBulkOp.find({_id: 1}).updateOne([{$set: {unorderedBulkOpUpdateOne: true}}]);
    unorderedBulkOp.find({}).update([{$set: {unorderedBulkOpUpdateMulti: true}}]);
    orderedBulkOp.find({_id: 1}).updateOne([{$set: {orderedBulkOpUpdateOne: true}}]);
    orderedBulkOp.find({}).update([{$set: {orderedBulkOpUpdateMulti: true}}]);
    assert.commandWorked(unorderedBulkOp.execute());
    assert.commandWorked(orderedBulkOp.execute());

    // Verify that the results of the various update operations are as expected.
    const observedResults = testColl.find().toArray();
    const expectedResults = [
        {
          _id: 1,
          a: 1,
          b: 2,
          update: true,
          updateMulti: true,
          updateOne: true,
          bulkWriteUpdateOne: true,
          bulkWriteUpdateMany: true,
          unorderedBulkOpUpdateOne: true,
          unorderedBulkOpUpdateMulti: true,
          orderedBulkOpUpdateOne: true,
          orderedBulkOpUpdateMulti: true
        },
        {
          _id: 2,
          a: 2,
          b: 3,
          updateMulti: true,
          bulkWriteUpdateMany: true,
          unorderedBulkOpUpdateMulti: true,
          orderedBulkOpUpdateMulti: true
        }
    ];
    assert(arrayEq(observedResults, expectedResults));

    // Test that findAndModify and associated helpers correctly handle pipeline syntax.
    const expectedFindAndModifyPostImage = Object.merge(expectedResults[0], {findAndModify: true});
    const expectedFindOneAndUpdatePostImage =
        Object.merge(expectedFindAndModifyPostImage, {findOneAndUpdate: true});
    const findAndModifyPostImage = testColl.findAndModify(
        {query: {_id: 1}, update: [{$set: {findAndModify: true}}], new: true});
    assert.docEq(findAndModifyPostImage, expectedFindAndModifyPostImage);
    const findOneAndUpdatePostImage = testColl.findOneAndUpdate(
        {_id: 1}, [{$set: {findOneAndUpdate: true}}], {returnNewDocument: true});
    assert.docEq(findOneAndUpdatePostImage, expectedFindOneAndUpdatePostImage);

    // Shell helpers for replacement updates should reject pipeline-style updates.
    assert.throws(() => testColl.replaceOne({_id: 1}, [{$replaceRoot: {newRoot: {}}}]));
    assert.throws(() => testColl.findOneAndReplace({_id: 1}, [{$replaceRoot: {newRoot: {}}}]));
    assert.throws(
        () => testColl.bulkWrite(
            [{replaceOne: {filter: {_id: 1}, replacement: [{$replaceRoot: {newRoot: {}}}]}}]));
})();