summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/out/out_in_lookup_not_allowed.js
blob: 9e97363233cc24106a4c36d406824ef0403e742a (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
// Tests that $out 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 ERROR_CODE_OUT_BANNED_IN_LOOKUP = 51047;
const ERROR_CODE_OUT_LAST_STAGE_ONLY = 40601;
const coll = db.out_in_lookup_not_allowed;
coll.drop();

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

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

let pipeline = [
        {
          $lookup: {
              pipeline: [{$out: "out_collection"}],
              from: from.getName(),
              as: "c",
          }
        },
    ];
assertErrorCode(coll, pipeline, ERROR_CODE_OUT_BANNED_IN_LOOKUP);

pipeline = [
        {
          $lookup: {
              pipeline: [{$project: {x: 0}}, {$out: "out_collection"}],
              from: from.getName(),
              as: "c",
          }
        },
    ];

assertErrorCode(coll, pipeline, ERROR_CODE_OUT_BANNED_IN_LOOKUP);

pipeline = [
        {
          $lookup: {
              pipeline: [{$out: "out_collection"}, {$match: {x: true}}],
              from: from.getName(),
              as: "c",
          }
        },
    ];

// Pipeline will fail because $out is not last in the subpipeline.
// Validation for $out in a $lookup's subpipeline occurs at a later point.
assertErrorCode(coll, pipeline, ERROR_CODE_OUT_LAST_STAGE_ONLY);

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

pipeline = [
        {
          $lookup: {
              pipeline: [{$out: "out_collection"}],
              from: from.getName(),
              as: "c",
          }
        },
    ];

// Pipeline will fail because $out is not allowed to exist within a $lookup.
// Validation for $out in a view occurs at a later point.
const cmdRes =
    coll.getDB().runCommand({create: "view1", viewOn: coll.getName(), pipeline: pipeline});
assert.commandFailedWithCode(cmdRes, ERROR_CODE_OUT_BANNED_IN_LOOKUP);
}());