summaryrefslogtreecommitdiff
path: root/jstests/change_streams/pipeline_style_updates.js
blob: 75c0e5fb1cf0c5ed94a1245f490a0bf2055b0f80 (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
/**
 * Test the change events generated by pipeline-based updates are expected with delta format oplog.
 *
 * @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.

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");

cst.cleanUp();
}());