summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/merge/disallowed_in_lookup.js
blob: 3ed6484076f496e43a8063b249f40f2412e2ccde (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
/**
 * Tests that $merge cannot be used within a $lookup pipeline.
 *
 * @tags: [requires_fcv_51]
 */
(function() {
"use strict";

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

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

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

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",
          }
        },
    ];
assertErrorCode(coll, pipeline, kErrorCodeMergeBannedInLookup);

// 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);
}());