summaryrefslogtreecommitdiff
path: root/jstests/core/geo_s2indexversion1.js
blob: bfb70e3a16c5b49baff70b2e4793a81d3a9a6883 (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
// Tests 2dsphere index option "2dsphereIndexVersion".  Verifies that GeoJSON objects that are new
// in version 2 are not allowed in version 1.

var coll = db.getCollection("geo_s2indexversion1");
coll.drop();

//
// Index build should fail for invalid values of "2dsphereIndexVersion".
//

var res;
res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": -1});
assert.commandFailed(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": 0});
assert.commandFailed(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": 3});
assert.commandFailed(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": Infinity});
assert.commandFailed(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": "foo"});
assert.commandFailed(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": {a: 1}});
assert.commandFailed(res);
coll.drop();

//
// Index build should succeed for valid values of "2dsphereIndexVersion".
//

res = coll.ensureIndex({geo: "2dsphere"});
assert.commandWorked(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": 1});
assert.commandWorked(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": NumberInt(1)});
assert.commandWorked(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": NumberLong(1)});
assert.commandWorked(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": 2});
assert.commandWorked(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": NumberInt(2)});
assert.commandWorked(res);
coll.drop();

res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": NumberLong(2)});
assert.commandWorked(res);
coll.drop();

//
// {2dsphereIndexVersion: 2} should be the default for new indexes.
//

res = coll.ensureIndex({geo: "2dsphere"});
assert.commandWorked(res);
var specObj = coll.getDB().system.indexes.findOne({ns: coll.getFullName(), name: "geo_2dsphere"});
assert.eq(2, specObj["2dsphereIndexVersion"]);
coll.drop();

//
// Test compatibility of various GeoJSON objects with both 2dsphere index versions.
//

var pointDoc = {geo: {type: "Point", coordinates: [40, 5]}};
var lineStringDoc = {geo: {type: "LineString", coordinates: [[40, 5], [41, 6]]}};
var polygonDoc = {geo: {type: "Polygon", coordinates: [[[0, 0], [3, 6], [6, 1], [0, 0]]]}};
var multiPointDoc = {geo: {type: "MultiPoint",
                           coordinates: [[-73.9580, 40.8003], [-73.9498, 40.7968],
                                         [-73.9737, 40.7648], [-73.9814, 40.7681]]}};
var multiLineStringDoc = {geo: {type: "MultiLineString",
                                coordinates: [[[-73.96943, 40.78519], [-73.96082, 40.78095]],
                                              [[-73.96415, 40.79229], [-73.95544, 40.78854]],
                                              [[-73.97162, 40.78205], [-73.96374, 40.77715]],
                                              [[-73.97880, 40.77247], [-73.97036, 40.76811]]]}};
var multiPolygonDoc = {geo: {type: "MultiPolygon",
                             coordinates: [[[[-73.958, 40.8003], [-73.9498, 40.7968],
                                             [-73.9737, 40.7648], [-73.9814, 40.7681],
                                             [-73.958, 40.8003]]],
                                           [[[-73.958, 40.8003], [-73.9498, 40.7968],
                                             [-73.9737, 40.7648], [-73.958, 40.8003]]]]}};
var geometryCollectionDoc = {geo: {type: "GeometryCollection",
                                   geometries: [{type: "MultiPoint",
                                                 coordinates: [[-73.9580, 40.8003],
                                                               [-73.9498, 40.7968],
                                                               [-73.9737, 40.7648],
                                                               [-73.9814, 40.7681]]},
                                                {type: "MultiLineString",
                                                 coordinates: [[[-73.96943, 40.78519],
                                                                [-73.96082, 40.78095]],
                                                               [[-73.96415, 40.79229],
                                                                [-73.95544, 40.78854]],
                                                               [[-73.97162, 40.78205],
                                                                [-73.96374, 40.77715]],
                                                               [[-73.97880, 40.77247],
                                                                [-73.97036, 40.76811]]]}]}};

// {2dsphereIndexVersion: 2} indexes allow all supported GeoJSON objects.
res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": 2});
assert.commandWorked(res);
res = coll.insert(pointDoc);
assert.writeOK(res);
res = coll.insert(lineStringDoc);
assert.writeOK(res);
res = coll.insert(polygonDoc);
assert.writeOK(res);
res = coll.insert(multiPointDoc);
assert.writeOK(res);
res = coll.insert(multiLineStringDoc);
assert.writeOK(res);
res = coll.insert(multiPolygonDoc);
assert.writeOK(res);
res = coll.insert(geometryCollectionDoc);
assert.writeOK(res);
coll.drop();

// {2dsphereIndexVersion: 1} indexes allow only Point, LineString, and Polygon.
res = coll.ensureIndex({geo: "2dsphere"}, {"2dsphereIndexVersion": 1});
assert.commandWorked(res);
res = coll.insert(pointDoc);
assert.writeOK(res);
res = coll.insert(lineStringDoc);
assert.writeOK(res);
res = coll.insert(polygonDoc);
assert.writeOK(res);
res = coll.insert(multiPointDoc);
assert.writeError(res);
res = coll.insert(multiLineStringDoc);
assert.writeError(res);
res = coll.insert(multiPolygonDoc);
assert.writeError(res);
res = coll.insert(geometryCollectionDoc);
assert.writeError(res);
coll.drop();