summaryrefslogtreecommitdiff
path: root/jstests/sharding/count1.js
blob: 2275faed65615a21d2951a4bf72a1939f6285d7f (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(function() {
    'use strict';

    var s = new ShardingTest({shards: 2});
    var db = s.getDB("test");

    // ************** Test Set #1 *************
    // Basic counts on "bar" collections, not yet sharded

    db.bar.save({n: 1});
    db.bar.save({n: 2});
    db.bar.save({n: 3});

    assert.eq(3, db.bar.find().count(), "bar 1");
    assert.eq(1, db.bar.find({n: 1}).count(), "bar 2");

    //************** Test Set #2 *************
    // Basic counts on sharded  "foo" collection.
    // 1. Create foo collection, insert 6 docs
    // 2. Divide into three chunks
    // 3. Test counts before chunk migrations
    // 4. Manually move chunks.  Now each shard should have 3 docs.
    // 5. i. Test basic counts on foo
    //    ii. Test counts with limit
    //    iii. Test counts with skip
    //    iv. Test counts with skip + limit
    //    v. Test counts with skip + limit + sorting
    // 6. Insert 10 more docs. Further limit/skip testing with a find query
    // 7. test invalid queries/values

    // part 1
    s.adminCommand({enablesharding: "test"});
    s.ensurePrimaryShard('test', s.shard1.shardName);
    s.adminCommand({shardcollection: "test.foo", key: {name: 1}});

    var primary = s.getPrimaryShard("test").getDB("test");
    var secondary = s.getOther(primary).getDB("test");

    assert.eq(1, s.config.chunks.count({"ns": "test.foo"}), "sanity check A");

    assert.commandWorked(db.foo.insert({_id: 1, name: "eliot"}));
    assert.commandWorked(db.foo.insert({_id: 2, name: "sara"}));
    assert.commandWorked(db.foo.insert({_id: 3, name: "bob"}));
    assert.commandWorked(db.foo.insert({_id: 4, name: "joe"}));
    assert.commandWorked(db.foo.insert({_id: 5, name: "mark"}));
    assert.commandWorked(db.foo.insert({_id: 6, name: "allan"}));

    assert.eq(6, db.foo.find().count(), "basic count");

    // part 2
    assert.commandWorked(s.s0.adminCommand({split: "test.foo", middle: {name: "allan"}}));
    assert.commandWorked(s.s0.adminCommand({split: "test.foo", middle: {name: "sara"}}));
    assert.commandWorked(s.s0.adminCommand({split: "test.foo", middle: {name: "eliot"}}));

    // MINKEY->allan,bob->eliot,joe,mark->sara,MAXKEY

    s.printChunks();

    // part 3
    assert.eq(6, db.foo.find().count(), "basic count after split ");
    assert.eq(6, db.foo.find().sort({name: 1}).count(), "basic count after split sorted ");

    // part 4
    assert.commandWorked(s.s0.adminCommand({
        moveChunk: "test.foo",
        find: {name: "eliot"},
        to: secondary.getMongo().name,
        _waitForDelete: true
    }));

    assert.eq(3, primary.foo.find().toArray().length, "primary count");
    assert.eq(3, secondary.foo.find().toArray().length, "secondary count");
    assert.eq(3, primary.foo.find().sort({name: 1}).toArray().length, "primary count sorted");
    assert.eq(3, secondary.foo.find().sort({name: 1}).toArray().length, "secondary count sorted");

    // part 5
    // Some redundant tests, but better safe than sorry. These are fast tests, anyway.

    // i.
    assert.eq(6, db.foo.find().count(), "total count after move");
    assert.eq(6, db.foo.find().toArray().length, "total count after move");
    assert.eq(6, db.foo.find().sort({name: 1}).toArray().length, "total count() sorted");
    assert.eq(6, db.foo.find().sort({name: 1}).count(), "total count with count() after move");

    // ii.
    assert.eq(2, db.foo.find().limit(2).count(true));
    assert.eq(2, db.foo.find().limit(-2).count(true));
    assert.eq(6, db.foo.find().limit(100).count(true));
    assert.eq(6, db.foo.find().limit(-100).count(true));
    assert.eq(6, db.foo.find().limit(0).count(true));

    // iii.
    assert.eq(6, db.foo.find().skip(0).count(true));
    assert.eq(5, db.foo.find().skip(1).count(true));
    assert.eq(4, db.foo.find().skip(2).count(true));
    assert.eq(3, db.foo.find().skip(3).count(true));
    assert.eq(2, db.foo.find().skip(4).count(true));
    assert.eq(1, db.foo.find().skip(5).count(true));
    assert.eq(0, db.foo.find().skip(6).count(true));
    assert.eq(0, db.foo.find().skip(7).count(true));

    // iv.
    assert.eq(2, db.foo.find().limit(2).skip(1).count(true));
    assert.eq(2, db.foo.find().limit(-2).skip(1).count(true));
    assert.eq(5, db.foo.find().limit(100).skip(1).count(true));
    assert.eq(5, db.foo.find().limit(-100).skip(1).count(true));
    assert.eq(5, db.foo.find().limit(0).skip(1).count(true));

    assert.eq(0, db.foo.find().limit(2).skip(10).count(true));
    assert.eq(0, db.foo.find().limit(-2).skip(10).count(true));
    assert.eq(0, db.foo.find().limit(100).skip(10).count(true));
    assert.eq(0, db.foo.find().limit(-100).skip(10).count(true));
    assert.eq(0, db.foo.find().limit(0).skip(10).count(true));

    assert.eq(2, db.foo.find().limit(2).itcount(), "LS1");
    assert.eq(2, db.foo.find().skip(2).limit(2).itcount(), "LS2");
    assert.eq(1, db.foo.find().skip(5).limit(2).itcount(), "LS3");
    assert.eq(6, db.foo.find().limit(2).count(), "LSC1");
    assert.eq(2, db.foo.find().limit(2).size(), "LSC2");
    assert.eq(2, db.foo.find().skip(2).limit(2).size(), "LSC3");
    assert.eq(1, db.foo.find().skip(5).limit(2).size(), "LSC4");
    assert.eq(4, db.foo.find().skip(1).limit(4).size(), "LSC5");
    assert.eq(5, db.foo.find().skip(1).limit(6).size(), "LSC6");

    // SERVER-3567 older negative limit tests
    assert.eq(2, db.foo.find().limit(2).itcount(), "N1");
    assert.eq(2, db.foo.find().limit(-2).itcount(), "N2");
    assert.eq(2, db.foo.find().skip(4).limit(2).itcount(), "N3");
    assert.eq(2, db.foo.find().skip(4).limit(-2).itcount(), "N4");

    // v.
    function nameString(c) {
        var s = "";
        while (c.hasNext()) {
            var o = c.next();
            if (s.length > 0)
                s += ",";
            s += o.name;
        }
        return s;
    }
    assert.eq("allan,bob,eliot,joe,mark,sara", nameString(db.foo.find().sort({name: 1})), "sort 1");
    assert.eq(
        "sara,mark,joe,eliot,bob,allan", nameString(db.foo.find().sort({name: -1})), "sort 2");

    assert.eq("allan,bob", nameString(db.foo.find().sort({name: 1}).limit(2)), "LSD1");
    assert.eq("bob,eliot", nameString(db.foo.find().sort({name: 1}).skip(1).limit(2)), "LSD2");
    assert.eq("joe,mark", nameString(db.foo.find().sort({name: 1}).skip(3).limit(2)), "LSD3");

    assert.eq("eliot,sara", nameString(db.foo.find().sort({_id: 1}).limit(2)), "LSE1");
    assert.eq("sara,bob", nameString(db.foo.find().sort({_id: 1}).skip(1).limit(2)), "LSE2");
    assert.eq("joe,mark", nameString(db.foo.find().sort({_id: 1}).skip(3).limit(2)), "LSE3");

    // part 6
    for (var i = 0; i < 10; i++) {
        assert.commandWorked(db.foo.insert({_id: 7 + i, name: "zzz" + i}));
    }

    assert.eq(10, db.foo.find({name: {$gt: "z"}}).itcount(), "LSF1");
    assert.eq(10, db.foo.find({name: {$gt: "z"}}).sort({_id: 1}).itcount(), "LSF2");
    assert.eq(5, db.foo.find({name: {$gt: "z"}}).sort({_id: 1}).skip(5).itcount(), "LSF3");
    assert.eq(3, db.foo.find({name: {$gt: "z"}}).sort({_id: 1}).skip(5).limit(3).itcount(), "LSF4");

    // part 7
    // Make sure count command returns error for invalid queries
    var badCmdResult = db.runCommand({count: 'foo', query: {$c: {$abc: 3}}});
    assert(!badCmdResult.ok, "invalid query syntax didn't return error");
    assert(badCmdResult.errmsg.length > 0, "no error msg for invalid query");

    // Negative skip values should return error
    var negSkipResult = db.runCommand({count: 'foo', skip: -2});
    assert(!negSkipResult.ok, "negative skip value shouldn't work");
    assert(negSkipResult.errmsg.length > 0, "no error msg for negative skip");

    // Negative skip values with positive limit should return error
    var negSkipLimitResult = db.runCommand({count: 'foo', skip: -2, limit: 1});
    assert(!negSkipLimitResult.ok, "negative skip value with limit shouldn't work");
    assert(negSkipLimitResult.errmsg.length > 0, "no error msg for negative skip");

    s.stop();
})();