summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/graphLookup/nested_objects.js
blob: d40cced2ac4e10045ee5c900c1fc5d9a0ff76f0a (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
// Cannot implicitly shard accessed collections because unsupported use of sharded collection
// for target collection of $lookup and $graphLookup.
// @tags: [assumes_unsharded_collection]

// In MongoDB 3.4, $graphLookup was introduced. In this file, we test the behavior of graphLookup
// when the 'connectToField' is a nested array, or when the 'connectFromField' is a nested array.

(function() {
    "use strict";

    var local = db.local;
    var foreign = db.foreign;

    local.drop();
    foreign.drop();

    // 'connectFromField' is an array of objects.
    var bulk = foreign.initializeUnorderedBulkOp();
    for (var i = 0; i < 100; i++) {
        bulk.insert({_id: i, neighbors: [{id: i + 1}, {id: i + 2}]});
    }
    assert.writeOK(bulk.execute());

    assert.writeOK(local.insert({starting: 0}));

    var res = local
                  .aggregate({
                      $graphLookup: {
                          from: "foreign",
                          startWith: "$starting",
                          connectFromField: "neighbors.id",
                          connectToField: "_id",
                          as: "integers"
                      }
                  })
                  .toArray()[0];
    assert.eq(res.integers.length, 100);

    foreign.drop();

    // 'connectToField' is an array of objects.
    var bulk = foreign.initializeUnorderedBulkOp();
    for (var i = 0; i < 100; i++) {
        bulk.insert({previous: [{neighbor: i}, {neighbor: i - 1}], value: i + 1});
    }
    assert.writeOK(bulk.execute());

    var res = local
                  .aggregate({
                      $graphLookup: {
                          from: "foreign",
                          startWith: "$starting",
                          connectFromField: "value",
                          connectToField: "previous.neighbor",
                          as: "integers"
                      }
                  })
                  .toArray()[0];
    assert.eq(res.integers.length, 100);

    foreign.drop();

    // Both 'connectToField' and 'connectFromField' are arrays of objects.
    var bulk = foreign.initializeUnorderedBulkOp();
    for (var i = 0; i < 100; i++) {
        bulk.insert({
            previous: [{neighbor: i}, {neighbor: i - 1}],
            values: [{neighbor: i + 1}, {neighbor: i + 2}]
        });
    }
    assert.writeOK(bulk.execute());

    var res = local
                  .aggregate({
                      $graphLookup: {
                          from: "foreign",
                          startWith: "$starting",
                          connectFromField: "values.neighbor",
                          connectToField: "previous.neighbor",
                          as: "integers"
                      }
                  })
                  .toArray()[0];
    assert.eq(res.integers.length, 100);
}());