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
|
// Test geoNear + $within.
t = db.geo_s2nearwithin
t.drop();
points = 10
for (var x = -points; x < points; x += 1) {
for (var y = -points; y < points; y += 1) {
t.insert({geo: [x, y]})
}
}
origin = { "type" : "Point", "coordinates": [ 0, 0] }
t.ensureIndex({ geo : "2dsphere" })
// Near requires an index, and 2dsphere is an index. Spherical isn't
// specified so this doesn't work.
resNear = db.runCommand({geoNear : t.getName(), near: [0, 0],
query: {geo: {$within: {$center: [[0, 0], 1]}}}})
assert(db.getLastError());
// Spherical is specified so this does work. Old style points are weird
// because you can use them with both $center and $centerSphere. Points are
// the only things we will do this conversion for.
resNear = db.runCommand({geoNear : t.getName(), near: [0, 0], spherical: true,
query: {geo: {$within: {$center: [[0, 0], 1]}}}})
assert.eq(resNear.results.length, 5)
resNear = db.runCommand({geoNear : t.getName(), near: [0, 0], spherical: true,
query: {geo: {$within: {$centerSphere: [[0, 0], Math.PI/180.0]}}}})
assert.eq(resNear.results.length, 5)
resNear = db.runCommand({geoNear : t.getName(), near: [0, 0], spherical: true,
query: {geo: {$within: {$centerSphere: [[0, 0], 0]}}}})
assert.eq(resNear.results.length, 1)
resNear = db.runCommand({geoNear : t.getName(), near: [0, 0], spherical: true,
query: {geo: {$within: {$centerSphere: [[1, 0], 0.5 * Math.PI/180.0]}}}})
assert.eq(resNear.results.length, 1)
resNear = db.runCommand({geoNear : t.getName(), near: [0, 0], spherical: true,
query: {geo: {$within: {$center: [[1, 0], 1.5]}}}})
assert.eq(resNear.results.length, 9)
|