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
|
// Test $near + $within.
(function() {
t = db.geo_nearwithin;
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]}));
}
}
assert.commandWorked(t.createIndex({geo: "2d"}));
const runQuery = (center) =>
t.find({$and: [{geo: {$near: [0, 0]}}, {geo: {$within: {$center: center}}}]}).toArray();
resNear = runQuery([[0, 0], 1]);
assert.eq(resNear.length, 5);
resNear = runQuery([[0, 0], 0]);
assert.eq(resNear.length, 1);
resNear = runQuery([[1, 0], 0.5]);
assert.eq(resNear.length, 1);
resNear = runQuery([[1, 0], 1.5]);
assert.eq(resNear.length, 9);
// 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);
}());
|