summaryrefslogtreecommitdiff
path: root/jstests/sharding/views.js
blob: 75a8eeda25829f88d57fde828be65c2c291ac2c3 (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
86
/**
 * Tests for expected behavior when querying a view that is based on a sharded collection.
 * @tags: [requires_find_command]
 */
(function() {
    "use strict";

    let st = new ShardingTest({name: "views_sharded", shards: 2, other: {enableBalancer: false}});

    let mongos = st.s;
    let config = mongos.getDB("config");
    let db = mongos.getDB(jsTestName());
    db.dropDatabase();

    let coll = db.getCollection("coll");

    assert.commandWorked(config.adminCommand({enableSharding: db.getName()}));
    st.ensurePrimaryShard(db.getName(), "shard0000");
    assert.commandWorked(config.adminCommand({shardCollection: coll.getFullName(), key: {a: 1}}));

    assert.commandWorked(mongos.adminCommand({split: coll.getFullName(), middle: {a: 6}}));
    assert.commandWorked(
        db.adminCommand({moveChunk: coll.getFullName(), find: {a: 25}, to: "shard0001"}));

    for (let i = 0; i < 10; ++i) {
        assert.writeOK(coll.insert({a: i}));
    }

    assert.commandWorked(db.createView("view", coll.getName(), [{$match: {a: {$gte: 4}}}]));
    let view = db.getCollection("view");

    //
    // find
    //
    assert.eq(5, view.find({a: {$lte: 8}}).itcount());

    let result = db.runCommand({explain: {find: "view", filter: {a: {$lte: 7}}}});
    assert.commandWorked(result);
    assert(result.hasOwnProperty("shards"), tojson(result));

    //
    // aggregate
    //
    assert.eq(5, view.aggregate([{$match: {a: {$lte: 8}}}]).itcount());

    result =
        db.runCommand({aggregate: "view", pipeline: [{$match: {a: {$lte: 8}}}], explain: true});
    assert.commandWorked(result);
    assert(result.hasOwnProperty("shards"), tojson(result));

    //
    // count
    //
    assert.eq(5, view.count({a: {$lte: 8}}));

    result = db.runCommand({explain: {count: "view", query: {a: {$lte: 8}}}});
    assert.commandWorked(result);
    assert(result.hasOwnProperty("shards"), tojson(result));

    //
    // distinct
    //
    result = db.runCommand({distinct: "view", key: "a", query: {a: {$lte: 8}}});
    assert.commandWorked(result);
    assert.eq([4, 5, 6, 7, 8], result.values.sort());

    result = db.runCommand({explain: {distinct: "view", key: "a", query: {a: {$lte: 8}}}});
    assert.commandWorked(result);
    assert(result.hasOwnProperty("shards"), tojson(result));

    //
    // Confirm cleanupOrphaned command fails.
    //
    result = st.getPrimaryShard(db.getName()).getDB("admin").runCommand({
        cleanupOrphaned: view.getFullName()
    });
    assert.commandFailedWithCode(result, ErrorCodes.CommandNotSupportedOnView);

    //
    //  Confirm getShardVersion command fails.
    //
    assert.commandFailedWithCode(db.adminCommand({getShardVersion: view.getFullName()}),
                                 ErrorCodes.NamespaceNotSharded);

    st.stop();
})();