summaryrefslogtreecommitdiff
path: root/jstests/core/geo_s2nearwithin.js
blob: 5d69399dc35bd56572da93bd573d59025ebe8369 (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
// Test $geoNear + $within.
(function() {
'use strict';

const t = db.geo_s2nearwithin;
t.drop();

assert.commandWorked(t.createIndex({geo: "2dsphere"}));

let docs = [];
let docId = 0;
const points = 10;
for (let x = -points; x < points; x += 1) {
    for (let y = -points; y < points; y += 1) {
        docs.push({_id: docId++, geo: [x, y]});
    }
}
assert.commandWorked(t.insert(docs));

const origin = {
    "type": "Point",
    "coordinates": [0, 0]
};

// 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.
const runGeoNear = (within) => t.aggregate({
                                    $geoNear: {
                                        near: [0, 0],
                                        distanceField: "d",
                                        spherical: true,
                                        query: {geo: {$within: within}},
                                    }
                                }).toArray();

let resNear = runGeoNear({$center: [[0, 0], 1]});
assert.eq(resNear.length, 5, tojson(resNear));

resNear = runGeoNear({$centerSphere: [[0, 0], Math.PI / 180.0]});
assert.eq(resNear.length, 5, tojson(resNear));

resNear = runGeoNear({$centerSphere: [[0, 0], 0]});
assert.eq(resNear.length, 1, tojson(resNear));

resNear = runGeoNear({$centerSphere: [[1, 0], 0.5 * Math.PI / 180.0]});
assert.eq(resNear.length, 1, tojson(resNear));

resNear = runGeoNear({$center: [[1, 0], 1.5]});
assert.eq(resNear.length, 9, tojson(resNear));

// Near requires an index, and 2dsphere is an index.  Spherical isn't
// specified so this doesn't work.
let res = assert.commandFailedWithCode(t.runCommand("aggregate", {
    cursor: {},
    pipeline: [{
        $geoNear:
            {near: [0, 0], distanceField: "d", query: {geo: {$within: {$center: [[0, 0], 1]}}}}
    }],
}),
                                       ErrorCodes.NoQueryExecutionPlans);
assert(res.errmsg.includes("unable to find index for $geoNear query"), tojson(res));
}());