summaryrefslogtreecommitdiff
path: root/jstests/core/geo_s2nearwithin.js
blob: a40aa34d25589cf62ad2bb8feedecea88bac0d1f (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
// Test $geoNear + $within.
(function() {
t = db.geo_s2nearwithin;
t.drop();

points = 10;
for (var x = -points; x < points; x += 1) {
    for (var y = -points; y < points; y += 1) {
        assert.commandWorked(t.insert({geo: [x, y]}));
    }
}

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

assert.commandWorked(t.ensureIndex({geo: "2dsphere"}));
// 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));

// 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();

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

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

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

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

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