summaryrefslogtreecommitdiff
path: root/jstests/sharding/graph_lookup.js
blob: bd3236f2c847f4aa4edb71484a614ca5c851a6d6 (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
// Test aggregating a sharded collection while using $graphLookup on an unsharded collection.
(function() {
    'use strict';

    const st = new ShardingTest({shards: 2, rs: {nodes: 1}});

    assert.commandWorked(st.s0.adminCommand({enablesharding: "test"}));
    assert.commandWorked(st.s0.adminCommand({shardCollection: "test.foo", key: {_id: "hashed"}}));

    let db = st.s0.getDB("test");

    assert.writeOK(db.foo.insert([{}, {}, {}, {}]));
    assert.writeOK(db.bar.insert({_id: 1, x: 1}));

    const res = db.foo
                    .aggregate([{
                        $graphLookup: {
                            from: "bar",
                            startWith: {$literal: 1},
                            connectFromField: "x",
                            connectToField: "_id",
                            as: "res"
                        }
                    }])
                    .toArray();

    assert.eq(res.length, 4);
    res.forEach(function(c) {
        assert.eq(c.res.length, 1);
        assert.eq(c.res[0]._id, 1);
        assert.eq(c.res[0].x, 1);
    });

    // Be sure $graphLookup is banned on sharded foreign collection.
    assert.commandWorked(st.s0.adminCommand({shardCollection: "test.baz", key: {_id: "hashed"}}));
    assert.commandWorked(db.baz.insert({_id: 1, x: 1}));
    const err = assert.throws(() => db.foo.aggregate([{
        $graphLookup: {
            from: "baz",
            startWith: {$literal: 1},
            connectFromField: "x",
            connectToField: "_id",
            as: "res"
        }
    }]));
    assert.eq(28769, err.code);

    st.stop();
})();