summaryrefslogtreecommitdiff
path: root/jstests/aggregation/variables/remove_system_variable.js
blob: 2ac85f1e213e7942932b3a06dbb84117c7a3ccd0 (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
/**
 * Tests for the $$REMOVE system variable.
 */
(function() {
"use strict";

let coll = db[jsTest.name()];
coll.drop();

assert.commandWorked(coll.insert({_id: 1, a: 2, b: 3}));
assert.commandWorked(coll.insert({_id: 2, a: 3, b: 4}));
assert.commandWorked(coll.insert({_id: 3, a: {b: 98, c: 99}}));

let projectStage = {
    $project: {_id: 0, a: 1, b: {$cond: {if: {$eq: ["$b", 4]}, then: "$$REMOVE", else: "$b"}}}
};

// Test that we can conditionally remove a field in $project.
assert.eq([{a: 2, b: 3}], coll.aggregate([{$match: {_id: 1}}, projectStage]).toArray());
assert.eq([{a: 3}], coll.aggregate([{$match: {_id: 2}}, projectStage]).toArray());

// Test removal of a nested field, using $project.
assert.eq([{a: {b: 98}}],
          coll.aggregate([{$match: {_id: 3}}, {$project: {_id: 0, "a.b": 1}}]).toArray());
assert.eq([{a: {}}],
          coll.aggregate([{$match: {_id: 3}}, {$project: {_id: 0, "a.b": "$$REMOVE"}}]).toArray());
assert.eq([{a: {}}],
          coll.aggregate([{$match: {_id: 3}}, {$project: {_id: 0, a: {b: "$$REMOVE"}}}]).toArray());

// Test removal of a nested field, using $addFields.
assert.eq([{_id: 3, a: {c: 99}}],
          coll.aggregate([{$match: {_id: 3}}, {$addFields: {"a.b": "$$REMOVE"}}]).toArray());

// Test that any field path following "$$REMOVE" also evaluates to missing.
assert.eq([{_id: 3}],
          coll.aggregate([{$match: {_id: 3}}, {$addFields: {"a": "$$REMOVE.a.c"}}]).toArray());

// Test that $$REMOVE can be used together with user-defined variables in a $let.
assert.eq(
    [{a: {b: 3, d: 4}}],
    coll.aggregate([
            {$match: {_id: 3}},
            {
                $project: {
                    _id: 0,
                    a: {
                        $let: {vars: {bar: 3, foo: 4}, in : {b: "$$bar", c: "$$REMOVE", d: "$$foo"}}
                    }
                }
            }
        ])
        .toArray());

// Test that $$REMOVE cannot be assigned in a $let.
assert.commandFailedWithCode(db.runCommand({
    aggregate: coll.getName(),
    cursor: {},
    pipeline: [
        {$match: {_id: 3}},
        {$project: {_id: 0, a: {$let: {vars: {"REMOVE": 3}, in : {b: "$$REMOVE", c: 2}}}}}
    ]
}),
                             16867);

// Test that $$REMOVE, $$CURRENT, $$ROOT, and user-defined variables can all be used together.
assert.eq(
        [{a: {b: 3, d: {_id: 1, a: 2, b: 3}, e: {_id: 1, a: 2, b: 3}}}],
        coll.aggregate([
                {$match: {_id: 1}},
                {
                  $project: {
                      _id: 0,
                      a: {
                          $let: {
                              vars: {myVar: 3},
                              in : {b: "$$myVar", c: "$$REMOVE", d: "$$ROOT", e: "$$CURRENT"}
                          }
                      }
                  }
                }
            ])
            .toArray());
}());