summaryrefslogtreecommitdiff
path: root/jstests/core/geo_nearwithin.js
blob: 34fc1edae5e7147085fb5b9b97cbcca20e91b206 (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
// Test $near + $within.
(function() {
'use strict';

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

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

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

const runQuery = (center) =>
    t.find({$and: [{geo: {$near: [0, 0]}}, {geo: {$within: {$center: center}}}]}).toArray();

let resNear = runQuery([[0, 0], 1]);
assert.eq(resNear.length, 5, tojson(resNear));

resNear = runQuery([[0, 0], 0]);
assert.eq(resNear.length, 1, tojson(resNear));

resNear = runQuery([[1, 0], 0.5]);
assert.eq(resNear.length, 1, tojson(resNear));

resNear = runQuery([[1, 0], 1.5]);
assert.eq(resNear.length, 9, tojson(resNear));

// We want everything distance >1 from us but <1.5
// These points are (-+1, -+1)
resNear = t.find({
               $and: [
                   {geo: {$near: [0, 0]}},
                   {geo: {$within: {$center: [[0, 0], 1.5]}}},
                   {geo: {$not: {$within: {$center: [[0, 0], 1]}}}}
               ]
           }).toArray();
assert.eq(resNear.length, 4, tojson(resNear));
}());