summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/graphLookup/variables.js
blob: 87e2c8b3975672d2abd01fc2f66fc88e42e97f8a (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
/**
 * Tests to verify that $graphLookup can use the variables defined in an outer scope.
 */
(function() {
    "use strict";

    let local = db.graph_lookup_var_local;
    let foreign = db.graph_lookup_var_foreign;
    local.drop();
    foreign.drop();

    foreign.insert({from: "b", to: "a", _id: 0});
    local.insert({});

    const basicGraphLookup = {
        $graphLookup: {
            from: "graph_lookup_var_foreign",
            startWith: "$$var1",
            connectFromField: "from",
            connectToField: "to",
            as: "resultsFromGraphLookup"
        }
    };

    const lookup = {
        $lookup: {
            from: "graph_lookup_var_local",
            let : {var1: "a"},
            pipeline: [basicGraphLookup],
            as: "resultsFromLookup"
        }
    };

    // Verify that $graphLookup can use the variable 'var1' which is defined in parent $lookup.
    let res = local.aggregate([lookup]).toArray();
    assert.eq(res.length, 1);
    assert.eq(res[0].resultsFromLookup.length, 1);
    assert.eq(res[0].resultsFromLookup[0].resultsFromGraphLookup.length, 1);
    assert.eq(res[0].resultsFromLookup[0].resultsFromGraphLookup[0], {_id: 0, from: "b", to: "a"});

})();