summaryrefslogtreecommitdiff
path: root/jstests/geo_mindistance.js
blob: b429eacb7088baa77c768dfb3970f24dd0f60819 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Test $minDistance option for $near and $nearSphere queries, and geoNear
 * command. SERVER-9395.
*/
var t = db.geo_mindistance;
t.drop();

//
// Useful constants and functions.
//

var km = 1000,
    earthRadiusMeters = 6378.1 * km;

function metersToRadians(m) { return m / earthRadiusMeters; }

/* Count documents within some radius of (0, 0), in kilometers.
 * With this function we can use the existing $maxDistance option to test
 * the newer $minDistance option's behavior.
 */
function n_docs_within(radius_km) {
    // geoNear's distances are in meters for geoJSON points.
    var cmdResult = db.runCommand({
        geoNear: t.getName(),
        near: {type: 'Point', coordinates: [0, 0]},
        spherical: true,
        maxDistance: radius_km * km,
        num: 1000
    });

    return cmdResult.results.length;
}

//
// Setup.
//

/* Make 121 points from long, lat = (0, 0) (in Gulf of Guinea) to (10, 10)
 * (inland Nigeria).
 */
for (var x = 0; x <= 10; x += 1) {
    for (var y = 0; y <= 10; y += 1) {
        t.insert({loc: [x, y]});
    }
}

/* $minDistance is supported for 2dsphere index only, not 2d or geoHaystack. */
t.ensureIndex({loc: "2dsphere"});

var n_docs = t.count(),
    geoJSONPoint = {type: 'Point', coordinates: [0, 0]},
    legacyPoint = [0, 0];

//
// Test $near with GeoJSON point (required for $near with 2dsphere index).
// min/maxDistance are in meters.
//

var n_min1400_count = t.find({loc: {
    $near: {$geometry: geoJSONPoint, $minDistance: 1400 * km
}}}).count();

assert.eq(
    n_docs - n_docs_within(1400),
    n_min1400_count,
    "Expected " + (n_docs - n_docs_within(1400))
        + " points $near (0, 0) with $minDistance 1400 km, got "
        + n_min1400_count
);

var n_bw500_and_1000_count = t.find({loc: {
    $near: {$geometry: geoJSONPoint,
            $minDistance: 500 * km,
            $maxDistance: 1000 * km
}}}).count();

assert.eq(
    n_docs_within(1000) - n_docs_within(500),
    n_bw500_and_1000_count,
    "Expected " + (n_docs_within(1000) - n_docs_within(500))
        + " points $near (0, 0) with $minDistance 500 km and $maxDistance 1000 km, got "
        + n_bw500_and_1000_count
);

//
// $nearSphere with 2dsphere index can take a legacy or GeoJSON point.
// First test $nearSphere with legacy point.
// min/maxDistance are in radians.
//

n_min1400_count = t.find({loc: {
    $nearSphere: legacyPoint, $minDistance: metersToRadians(1400 * km)
}}).count();

assert.eq(
    n_docs - n_docs_within(1400),
    n_min1400_count,
    "Expected " + (n_docs - n_docs_within(1400))
        + " points $nearSphere (0, 0) with $minDistance 1400 km, got "
        + n_min1400_count
);

n_bw500_and_1000_count = t.find({loc: {
    $nearSphere: legacyPoint,
    $minDistance: metersToRadians(500 * km),
    $maxDistance: metersToRadians(1000 * km)
}}).count();

assert.eq(
    n_docs_within(1000) - n_docs_within(500),
    n_bw500_and_1000_count,
    "Expected " + (n_docs_within(1000) - n_docs_within(500))
        + " points $nearSphere (0, 0) with $minDistance 500 km and $maxDistance 1000 km, got "
        + n_bw500_and_1000_count
);

//
// Test $nearSphere with GeoJSON point.
// min/maxDistance are in meters.
//

n_min1400_count = t.find({loc: {
    $nearSphere: geoJSONPoint, $minDistance: 1400 * km
}}).count();

assert.eq(
    n_docs - n_docs_within(1400),
    n_min1400_count,
    "Expected " + (n_docs - n_docs_within(1400))
        + " points $nearSphere (0, 0) with $minDistance 1400 km, got "
        + n_min1400_count
);

n_bw500_and_1000_count = t.find({loc: {
    $nearSphere: geoJSONPoint,
    $minDistance: 500 * km,
    $maxDistance: 1000 * km
}}).count();

assert.eq(
    n_docs_within(1000) - n_docs_within(500),
    n_bw500_and_1000_count,
    "Expected " + (n_docs_within(1000) - n_docs_within(500))
        + " points $nearSphere (0, 0) with $minDistance 500 km and $maxDistance 1000 km, got "
        + n_bw500_and_1000_count
);


//
// Test geoNear command with GeoJSON point.
// Distances are in meters.
//

var cmdResult = db.runCommand({
    geoNear: t.getName(),
    near: {type: 'Point', coordinates: [0, 0]},
    minDistance: 1400 * km,
    spherical: true  // spherical required for 2dsphere index
});
assert.eq(
    n_docs - n_docs_within(1400),
    cmdResult.results.length,
    "Expected " + (n_docs - n_docs_within(1400))
        + " points geoNear (0, 0) with $minDistance 1400 km, got "
        + cmdResult.results.length
);

cmdResult = db.runCommand({
    geoNear: t.getName(),
    near: {type: 'Point', coordinates: [0, 0]},
    minDistance: 500 * km,
    maxDistance: 1000 * km,
    spherical: true
});
assert.eq(
    n_docs_within(1000) - n_docs_within(500),
    cmdResult.results.length,
    "Expected " + (n_docs_within(1000) - n_docs_within(500))
        + " points geoNear (0, 0) with $minDistance 500 km and $maxDistance 1000 km, got "
        + cmdResult.results.length
);

//
// Test geoNear command with legacy point.
// Distances are in radians.
//

cmdResult = db.runCommand({
    geoNear: t.getName(),
    near: legacyPoint,
    minDistance: metersToRadians(1400 * km),
    spherical: true  // spherical required for 2dsphere index
});
assert.eq(
    n_docs - n_docs_within(1400),
    cmdResult.results.length,
    "Expected " + (n_docs - n_docs_within(1400))
        + " points geoNear (0, 0) with $minDistance 1400 km, got "
        + cmdResult.results.length
);

cmdResult = db.runCommand({
    geoNear: t.getName(),
    near: legacyPoint,
    minDistance: metersToRadians(500 * km),
    maxDistance: metersToRadians(1000 * km),
    spherical: true
});
assert.eq(
    n_docs_within(1000) - n_docs_within(500),
    cmdResult.results.length,
    "Expected " + (n_docs_within(1000) - n_docs_within(500))
        + " points geoNear (0, 0) with $minDistance 500 km and $maxDistance 1000 km, got "
        + cmdResult.results.length
);