summaryrefslogtreecommitdiff
path: root/jstests/core/geo_s2sparse.js
blob: 1a9a3b67b9bdc00b4e31cc7e82fbc7ac0d3191c2 (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
// Test behavior of 2dsphere and sparse.  See SERVER-9639.
// All V2 2dsphere indices are sparse in the geo fields.

(function() {
"use strict";

var coll = db.geo_s2sparse;
var point = {type: "Point", coordinates: [5, 5]};
var indexSpec = {geo: "2dsphere", nonGeo: 1};
var indexName = 'geo_2dsphere_nonGeo_1';

//
// V2 indices are "geo sparse" always.
//

// Clean up.
coll.drop();
coll.createIndex(indexSpec);

var bulkInsertDocs = function(coll, numDocs, makeDocFn) {
    print("Bulk inserting " + numDocs + " documents");

    var bulk = coll.initializeUnorderedBulkOp();
    for (var i = 0; i < numDocs; ++i) {
        bulk.insert(makeDocFn(i));
    }

    assert.commandWorked(bulk.execute());

    print("Bulk inserting " + numDocs + " documents completed");
};

// Insert N documents with the geo field.
var N = 1000;
bulkInsertDocs(coll, N, function(i) {
    return {geo: point, nonGeo: "point_" + i};
});

// Expect N keys.
assert.eq(N, coll.validate().keysPerIndex[indexName]);

// Insert N documents without the geo field.
bulkInsertDocs(coll, N, function(i) {
    return {wrongGeo: point, nonGeo: i};
});

// Still expect N keys as we didn't insert any geo stuff.
assert.eq(N, coll.validate().keysPerIndex[indexName]);

// Insert N documents with just the geo field.
bulkInsertDocs(coll, N, function(i) {
    return {geo: point};
});

// Expect 2N keys.
assert.eq(N + N, coll.validate().keysPerIndex[indexName]);

// Add some "not geo" stuff.
bulkInsertDocs(coll, N, function(i) {
    return {geo: null};
});
bulkInsertDocs(coll, N, function(i) {
    return {geo: []};
});
bulkInsertDocs(coll, N, function(i) {
    return {geo: undefined};
});
bulkInsertDocs(coll, N, function(i) {
    return {geo: {}};
});

// Still expect 2N keys.
assert.eq(N + N, coll.validate().keysPerIndex[indexName]);

//
// V1 indices are never sparse
//

coll.drop();
coll.createIndex(indexSpec, {"2dsphereIndexVersion": 1});

// Insert N documents with the geo field.
bulkInsertDocs(coll, N, function(i) {
    return {geo: point, nonGeo: "point_" + i};
});

// Expect N keys.
assert.eq(N, coll.validate().keysPerIndex[indexName]);

// Insert N documents without the geo field.
bulkInsertDocs(coll, N, function(i) {
    return {wrongGeo: point, nonGeo: i};
});

// Expect N keys as it's a V1 index.
assert.eq(N + N, coll.validate().keysPerIndex[indexName]);

//
// V2 indices with several 2dsphere-indexed fields are only sparse if all are missing.
//

// Clean up.
coll.drop();
coll.createIndex({geo: "2dsphere", otherGeo: "2dsphere"});

indexName = 'geo_2dsphere_otherGeo_2dsphere';

// Insert N documents with the first geo field.
bulkInsertDocs(coll, N, function(i) {
    return {geo: point};
});

// Expect N keys.
assert.eq(N, coll.validate().keysPerIndex[indexName]);

// Insert N documents with the second geo field.
bulkInsertDocs(coll, N, function(i) {
    return {otherGeo: point};
});

// They get inserted too.
assert.eq(N + N, coll.validate().keysPerIndex[indexName]);

// Insert N documents with neither geo field.
bulkInsertDocs(coll, N, function(i) {
    return {nonGeo: i};
});

// Still expect 2N keys as the neither geo docs were omitted from the index.
assert.eq(N + N, coll.validate().keysPerIndex[indexName]);
})();