summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/geo_mnypts_plus_fields.js
blob: eb8a03ce7397e2579625f3481fd4ef6b4e4b2e02 (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
// Test sanity of geo queries with a lot of points

load("jstests/libs/slow_weekly_util.js");
testServer = new SlowWeeklyMongod("geo_mnypts_plus_fields");
db = testServer.getDB("test");

var maxFields = 3;

for (var fields = 1; fields < maxFields; fields++) {
    var coll = db.testMnyPts;
    coll.drop();

    var totalPts = 500 * 1000;

    var bulk = coll.initializeUnorderedBulkOp();
    // Add points in a 100x100 grid
    for (var i = 0; i < totalPts; i++) {
        var ii = i % 10000;

        var doc = {loc: [ii % 100, Math.floor(ii / 100)]};

        // Add fields with different kinds of data
        for (var j = 0; j < fields; j++) {
            var field = null;

            if (j % 3 == 0) {
                // Make half the points not searchable
                field = "abcdefg" + (i % 2 == 0 ? "h" : "");
            } else if (j % 3 == 1) {
                field = new Date();
            } else {
                field = true;
            }

            doc["field" + j] = field;
        }

        bulk.insert(doc);
    }
    assert.writeOK(bulk.execute());

    // Create the query for the additional fields
    queryFields = {};
    for (var j = 0; j < fields; j++) {
        var field = null;

        if (j % 3 == 0) {
            field = "abcdefg";
        } else if (j % 3 == 1) {
            field = {$lte: new Date()};
        } else {
            field = true;
        }

        queryFields["field" + j] = field;
    }

    coll.ensureIndex({loc: "2d"});

    // Check that quarter of points in each quadrant
    for (var i = 0; i < 4; i++) {
        var x = i % 2;
        var y = Math.floor(i / 2);

        var box = [[0, 0], [49, 49]];
        box[0][0] += (x == 1 ? 50 : 0);
        box[1][0] += (x == 1 ? 50 : 0);
        box[0][1] += (y == 1 ? 50 : 0);
        box[1][1] += (y == 1 ? 50 : 0);

        // Now only half of each result comes back
        assert.eq(totalPts / (4 * 2),
                  coll.find(Object.extend({loc: {$within: {$box: box}}}, queryFields)).count());
        assert.eq(totalPts / (4 * 2),
                  coll.find(Object.extend({loc: {$within: {$box: box}}}, queryFields)).itcount());
    }

    // Check that half of points in each half
    for (var i = 0; i < 2; i++) {
        var box = [[0, 0], [49, 99]];
        box[0][0] += (i == 1 ? 50 : 0);
        box[1][0] += (i == 1 ? 50 : 0);

        assert.eq(totalPts / (2 * 2),
                  coll.find(Object.extend({loc: {$within: {$box: box}}}, queryFields)).count());
        assert.eq(totalPts / (2 * 2),
                  coll.find(Object.extend({loc: {$within: {$box: box}}}, queryFields)).itcount());
    }

    // Check that all but corner set of points in radius
    var circle = [[0, 0], (100 - 1) * Math.sqrt(2) - 0.25];

    // All [99,x] pts are field0 : "abcdefg"
    assert.eq(totalPts / 2 - totalPts / (100 * 100),
              coll.find(Object.extend({loc: {$within: {$center: circle}}}, queryFields)).count());
    assert.eq(totalPts / 2 - totalPts / (100 * 100),
              coll.find(Object.extend({loc: {$within: {$center: circle}}}, queryFields)).itcount());
}

testServer.stop();