summaryrefslogtreecommitdiff
path: root/jstests/change_streams/pipeline_style_updates.js
blob: 45aa3374cf13cf54380993fd50aedecca7f7c369 (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
/**
 * Test the change events generated by pipeline-based updates are expected with delta format oplog
 * enabled and disabled.
 *
 * @tags: [
 * ]
 */

(function() {
"use strict";

load("jstests/libs/change_stream_util.js");        // For ChangeStreamTest
load("jstests/libs/collection_drop_recreate.js");  // For assert[Drop|Create]Collection.
load("jstests/libs/discover_topology.js");         // For findNonConfigNodes.
load("jstests/noPassthrough/libs/server_parameter_helpers.js");  // For setParameterOnAllHosts.

const v2OplogEntriesServerParameter = "internalQueryEnableLoggingV2OplogEntries";
const defaultOpLogMode = getParameter(db.getMongo(), v2OplogEntriesServerParameter);
try {
    jsTestLog("Testing when $v:2 oplog entry is enabled.");
    setParameterOnAllHosts(
        DiscoverTopology.findNonConfigNodes(db.getMongo()), v2OplogEntriesServerParameter, true);

    assertDropAndRecreateCollection(db, "t1");

    const kLargeStr = '*'.repeat(512);

    assert.commandWorked(db.t1.insert({
        _id: 100,
        "a": 1,
        "b": 2,
        "obj": {"a": 1, "b": 2, "str": kLargeStr},
    }));

    const cst = new ChangeStreamTest(db);
    const changeStreamCursor =
        cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});

    function testPipelineStyleUpdate(pipeline, expectedChange, operationType) {
        assert.commandWorked(db.t1.update({_id: 100}, pipeline));
        const expected = Object.assign({
            documentKey: {_id: 100},
            ns: {db: "test", coll: "t1"},
            operationType: operationType,
        },
                                       expectedChange);
        cst.assertNextChangesEqual({cursor: changeStreamCursor, expectedChanges: [expected]});
    }

    jsTestLog("Testing pipeline-based update with $set.");
    let updatePipeline = [{$set: {a: 2}}];
    let expected = {
        updateDescription: {
            updatedFields: {"a": 2},
            removedFields: [],
            truncatedArrays: [],
        },
    };
    testPipelineStyleUpdate(updatePipeline, expected, "update");

    jsTestLog("Testing pipeline-based update with $unset.");
    updatePipeline = [{$unset: ["a"]}];
    expected = {
        updateDescription: {
            updatedFields: {},
            removedFields: ["a"],
            truncatedArrays: [],
        },
    };
    testPipelineStyleUpdate(updatePipeline, expected, "update");

    jsTestLog("Testing pipeline-based update with $replaceRoot.");
    updatePipeline =
        [{$replaceRoot: {newRoot: {_id: 100, b: 2, "obj": {"a": 2, "b": 2, "str": kLargeStr}}}}];
    expected = {
        updateDescription: {
            updatedFields: {"obj.a": 2},
            removedFields: [],
            truncatedArrays: [],
        },
    };
    testPipelineStyleUpdate(updatePipeline, expected, "update");

    jsTestLog("Testing when $v:2 oplog entry is disabled.");
    setParameterOnAllHosts(
        DiscoverTopology.findNonConfigNodes(db.getMongo()), v2OplogEntriesServerParameter, false);

    jsTestLog("Testing pipeline-based update with $set.");
    updatePipeline = [{$set: {a: 2}}];
    expected = {
        fullDocument: {
            _id: 100,
            "a": 2,
            "b": 2,
            "obj": {"a": 2, "b": 2, "str": kLargeStr},
        },
    };
    testPipelineStyleUpdate(updatePipeline, expected, "replace");

    jsTestLog("Testing pipeline-based update with $unset.");
    updatePipeline = [{$unset: ["a"]}];
    delete expected.fullDocument.a;
    testPipelineStyleUpdate(updatePipeline, expected, "replace");

    jsTestLog("Testing pipeline-based update with $replaceRoot.");
    updatePipeline = [{$replaceRoot: {newRoot: {_id: 100, "a": 1, "b": 2}}}];
    expected = {
        fullDocument: {
            _id: 100,
            "a": 1,
            "b": 2,
        },
    };
    testPipelineStyleUpdate(updatePipeline, expected, "replace");

    cst.cleanUp();
} finally {
    // Reset the server parameter to the original value, so that other tests running in the same
    // suite will not be impacted.
    setParameterOnAllHosts(DiscoverTopology.findNonConfigNodes(db.getMongo()),
                           v2OplogEntriesServerParameter,
                           defaultOpLogMode);
}
}());