summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/merge/merge_to_referenced_collection.js
blob: a9060f58b0a500036f4815d67e14db7ebb2c9e51 (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
/**
 * Tests that the server behaves as expected when an $merge stage is targeting a collection which is
 * involved in the aggregate in some other way, e.g. as the source namespace or via a $lookup. We
 * disallow this combination in an effort to prevent the "halloween problem" of a never-ending
 * query.
 *
 * This test issues queries over views, so cannot be run in passthroughs which implicitly shard
 * collections.
 * @tags: [assumes_unsharded_collection]
 */
(function() {
'use strict';

load('jstests/aggregation/extras/merge_helpers.js');  // For 'withEachMergeMode'.
load('jstests/libs/fixture_helpers.js');              // For 'FixtureHelpers'.

const testDB = db.getSiblingDB("merge_to_referenced_coll");
const coll = testDB.test;

withEachMergeMode(({whenMatchedMode, whenNotMatchedMode}) => {
    coll.drop();

    // Seed the collection to ensure each pipeline will actually do something.
    assert.commandWorked(coll.insert({_id: 0}));

    // Each of the following assertions will somehow use $merge to write to a namespace that is
    // being read from elsewhere in the pipeline.
    const assertFailsWithCode = ((fn) => {
        const error = assert.throws(fn);
        assert.contains(error.code, [51188, 51079]);
    });

    // Test $merge to the aggregate command's source collection.
    assertFailsWithCode(() => coll.aggregate([{
        $merge:
            {into: coll.getName(), whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}
    }]));

    // Test $merge to the same namespace as a $lookup which is the same as the aggregate
    // command's source collection.
    assertFailsWithCode(() => coll.aggregate([
        {$lookup: {from: coll.getName(), as: "x", localField: "f_id", foreignField: "_id"}},
        {
            $merge: {
                into: coll.getName(),
                whenMatched: whenMatchedMode,
                whenNotMatched: whenNotMatchedMode
            }
        }
    ]));

    // Test $merge to the same namespace as a $lookup which is *not* the same as the aggregate
    // command's source collection.
    assertFailsWithCode(() => coll.aggregate([
        {$lookup: {from: "bar", as: "x", localField: "f_id", foreignField: "_id"}},
        {$merge: {into: "bar", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}}
    ]));

    // Test $merge to the same namespace as a $graphLookup.
    assertFailsWithCode(() => coll.aggregate([
            {
              $graphLookup: {
                  from: "bar",
                  startWith: "$_id",
                  connectFromField: "_id",
                  connectToField: "parent_id",
                  as: "graph",
              }
            },
            {
              $merge: {
                  into: "bar",
                  whenMatched: whenMatchedMode,
                  whenNotMatched: whenNotMatchedMode
              }
            }
        ]));

    // Test $merge to the same namespace as a $lookup which is nested within another $lookup.
    assertFailsWithCode(() => coll.aggregate([
            {
              $lookup: {
                  from: "bar",
                  as: "x",
                  let : {},
                  pipeline: [{$lookup: {from: "TARGET", as: "y", pipeline: []}}]
              }
            },
            {
              $merge: {
                  into: "TARGET",
                  whenMatched: whenMatchedMode,
                  whenNotMatched: whenNotMatchedMode
              }
            }
        ]));
    // Test $merge to the same namespace as a $lookup which is nested within a $facet.
    assertFailsWithCode(() => coll.aggregate([
        {
            $facet: {
                y: [{$lookup: {from: "TARGET", as: "y", pipeline: []}}],
            }
        },
        {$merge: {into: "TARGET", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}}
    ]));
    assertFailsWithCode(() => coll.aggregate([
        {
            $facet: {
                x: [{$lookup: {from: "other", as: "y", pipeline: []}}],
                y: [{$lookup: {from: "TARGET", as: "y", pipeline: []}}],
            }
        },
        {$merge: {into: "TARGET", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}}
    ]));

    // Test that we use the resolved namespace of a view to detect this sort of halloween
    // problem.
    assert.commandWorked(
        testDB.runCommand({create: "view_on_TARGET", viewOn: "TARGET", pipeline: []}));
    assertFailsWithCode(() => testDB.view_on_TARGET.aggregate([
        {$merge: {into: "TARGET", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}}
    ]));
    assertFailsWithCode(() => coll.aggregate([
            {
              $facet: {
                  x: [{$lookup: {from: "other", as: "y", pipeline: []}}],
                  y: [{
                      $lookup: {
                          from: "yet_another",
                          as: "y",
                          pipeline: [{$lookup: {from: "view_on_TARGET", as: "z", pipeline: []}}]
                      }
                  }],
              }
            },
            {
              $merge: {
                  into: "TARGET",
                  whenMatched: whenMatchedMode,
                  whenNotMatched: whenNotMatchedMode
              }
            }
        ]));

    function generateNestedPipeline(foreignCollName, numLevels) {
        let pipeline = [{"$lookup": {pipeline: [], from: foreignCollName, as: "same"}}];

        for (let level = 1; level < numLevels; level++) {
            pipeline = [{"$lookup": {pipeline: pipeline, from: foreignCollName, as: "same"}}];
        }

        return pipeline;
    }

    const nestedPipeline = generateNestedPipeline("lookup", 20).concat([
        {$merge: {into: "lookup", whenMatched: whenMatchedMode, whenNotMatched: whenNotMatchedMode}}
    ]);
    assertFailsWithCode(() => coll.aggregate(nestedPipeline));

    testDB.dropDatabase();
});
}());