summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/merge/disallowed_in_lookup.js
blob: 19f37305dbeb6a9337d0ca0ac341754b8035787a (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
// Tests that $merge cannot be used within a $lookup pipeline.
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");                     // For assertErrorCode.
load("jstests/libs/collection_drop_recreate.js");                // For assertDropCollection.
load("jstests/noPassthrough/libs/server_parameter_helpers.js");  // For setParameterOnAllHosts.
load("jstests/libs/discover_topology.js");                       // For findNonConfigNodes.
load("jstests/libs/fixture_helpers.js");                         // For isSharded.

const kErrorCodeMergeBannedInLookup = 51047;
const kErrorCodeMergeLastStageOnly = 40601;
const coll = db.merge_in_lookup_not_allowed;
coll.drop();

const from = db.merge_in_lookup_not_allowed_from;
from.drop();

if (FixtureHelpers.isSharded(from)) {
    setParameterOnAllHosts(DiscoverTopology.findNonConfigNodes(db.getMongo()),
                           "internalQueryAllowShardedLookup",
                           true);
}

let pipeline = [
        {
          $lookup: {
              pipeline: [{$merge: {into: "out_collection", on: "_id"}}],
              from: from.getName(),
              as: "c",
          }
        },
    ];
assertErrorCode(coll, pipeline, kErrorCodeMergeBannedInLookup);

pipeline = [
        {
          $lookup: {
              pipeline: [{$project: {x: 0}}, {$merge: {into: "out_collection", on: "_id"}}],
              from: from.getName(),
              as: "c",
          }
        },
    ];
assertErrorCode(coll, pipeline, kErrorCodeMergeBannedInLookup);

pipeline = [
        {
          $lookup: {
              pipeline: [{$merge: {into: "out_collection", on: "_id"}}, {$match: {x: true}}],
              from: from.getName(),
              as: "c",
          }
        },
    ];
// Pipeline will fail because $merge is not last in the subpipeline.
// Validation for $merge in a $lookup's subpipeline occurs at a later point.
assertErrorCode(coll, pipeline, kErrorCodeMergeLastStageOnly);

// Create view which contains $merge within $lookup.
assertDropCollection(coll.getDB(), "view1");

pipeline = [
        {
          $lookup: {
              pipeline: [{$merge: {into: "out_collection", on: "_id"}}],
              from: from.getName(),
              as: "c",
          }
        },
    ];
// Pipeline will fail because $merge is not allowed to exist within a $lookup.
// Validation for $merge in a view occurs at a later point.
const cmdRes =
    coll.getDB().runCommand({create: "view1", viewOn: coll.getName(), pipeline: pipeline});
assert.commandFailedWithCode(cmdRes, kErrorCodeMergeBannedInLookup);

// Test that a $merge without an explicit "on" field still fails within a $lookup.
pipeline = [
        {
          $lookup: {
              pipeline: [{$merge: {into: "out_collection"}}],
              from: from.getName(),
              as: "c",
          }
        },
    ];
assert.commandFailedWithCode(
    db.runCommand({aggregate: coll.getName(), pipeline: pipeline, cursor: {}}),
    kErrorCodeMergeBannedInLookup);
}());