summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/graphLookup/sharded.js
blob: d07e596e609adc97c3ff4a6c424dd0f4beaa3de9 (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
// In SERVER-23725, $graphLookup was introduced. In this file, we test that the expression behaves
// @tags: [
//   requires_spawning_own_processes,
// ]
// correctly on a sharded collection.
load("jstests/aggregation/extras/utils.js");  // For assertErrorCode.

(function() {
    "use strict";

    var st = new ShardingTest({name: "aggregation_graph_lookup", shards: 2, mongos: 1});

    st.adminCommand({enableSharding: "graphLookup"});
    st.ensurePrimaryShard("graphLookup", "shard0001");
    st.adminCommand({shardCollection: "graphLookup.local", key: {_id: 1}});

    var foreign = st.getDB("graphLookup").foreign;
    var local = st.getDB("graphLookup").local;

    var bulk = foreign.initializeUnorderedBulkOp();

    for (var i = 0; i < 100; i++) {
        bulk.insert({_id: i, next: i + 1});
    }
    assert.writeOK(bulk.execute());

    assert.writeOK(local.insert({}));

    var res = st.s.getDB("graphLookup")
                  .local
                  .aggregate({
                      $graphLookup: {
                          from: "foreign",
                          startWith: {$literal: 0},
                          connectToField: "_id",
                          connectFromField: "next",
                          as: "number_line"
                      }
                  })
                  .toArray();

    assert.eq(res.length, 1);
    assert.eq(res[0].number_line.length, 100);

    // Cannot perform a $graphLookup where the "from" collection is sharded.
    var pipeline = {
        $graphLookup: {
            from: "local",
            startWith: {$literal: 0},
            connectToField: "_id",
            connectFromField: "_id",
            as: "out"
        }
    };

    assertErrorCode(foreign, pipeline, 28769);
    st.stop();
}());