summaryrefslogtreecommitdiff
path: root/jstests/aggregation/variables/layered_variables.js
blob: e0e10494b290da122877e6d3c64b230491dbb46e (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
// Tests that a pipeline with a blend of variable-using expressions reports correct results.

(function() {
    "use strict";
    const testDB = db.getSiblingDB("layered_variables");
    assert.commandWorked(testDB.dropDatabase());
    const coll = testDB.getCollection("test");

    assert.writeOK(coll.insert({_id: 1, has_permissions: 1, my_array: [2, 3]}));

    const res = assert.commandWorked(testDB.runCommand({
        aggregate: "test",
        pipeline: [
            {
              $addFields: {
                  a: 1,
                  b: {
                      $reduce: {
                          input: "$my_array",
                          initialValue: 1,
                          in : {$multiply: ["$$value", "$$this"]}
                      }
                  },
                  c: {$filter: {input: "$my_array", as: "filter", cond: {$gte: ["$$filter", 0]}}},
                  d: {
                      $let: {
                          vars: {two: 2, three: 3},
                          in : {
                              $multiply: [
                                  "$$two",
                                  "$$three",
                                  {
                                    $let: {
                                        // Variable shadowing here is intentional. It confirms that
                                        // the localy defined variables are used over ones defined
                                        // in the outer scope.
                                        vars: {two: 200, three: 300},
                                        in : {$add: ["$$two", "$$three"]}
                                    }
                                  }

                              ]
                          }
                      }
                  }
              }
            },
            {$redact: {$cond: {if: "$has_permissions", then: "$$DESCEND", else: "$$PRUNE"}}},
            {$addFields: {e: {$map: {input: "$my_array", as: "val", in : {$add: ["$$val", 1]}}}}},
        ],
        cursor: {}
    }));

    assert.eq(
        {_id: 1, has_permissions: 1, my_array: [2, 3], a: 1, b: 6, c: [2, 3], d: 3000, e: [3, 4]},
        res.cursor.firstBatch[0],
        tojson(res));
})();