Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 3dbebe6

Browse files
author
Hans Kristian Flaatten
committed
feat(query): add support for max and min options for $near query
1 parent c93d585 commit 3dbebe6

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ useful when building an API and accepting various user specificed queries.
5252
|-----------|---------------|--------------|
5353
| bbox | `?bbox=0,1,2,3` | `{ geojson: { $geoWithin: { $geometry: { … } } } }` |
5454
| near | `?near=0,1` | `{ geojson: { $near: { $geometry: { … } } } }` |
55+
| near (max distance) | `?near=0,1,2` | `{ geojson: { $near: { …, $maxDistance: 2 } } }` |
56+
| near (max & min distance) | `?near=0,1,2,3` | `{ geojson: { $near: { …, $minDistance: 3 } } }` |
5557

5658
* Custom query functions
5759
* `after` (date)

examples/test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ describe('Example App', function() {
5858
.end(done);
5959
});
6060

61+
it('returns places near point with max distance', function(done) {
62+
app.get(url + '?near=6.13037,61.00607,7000')
63+
.expect(200)
64+
.expect(function(res) {
65+
assert.equal(res.body.length, 2);
66+
assert.equal(res.body[0].name, 'Solrenningen');
67+
assert.equal(res.body[1].name, 'Åsedalen');
68+
})
69+
.end(done);
70+
});
71+
72+
it('returns places near point with max and min distance', function(done) {
73+
app.get(url + '?near=6.13037,61.00607,7000,1000')
74+
.expect(200)
75+
.expect(function(res) {
76+
assert.equal(res.body.length, 1);
77+
assert.equal(res.body[0].name, 'Åsedalen');
78+
})
79+
.end(done);
80+
});
81+
6182
it('returns places inside bbox', function(done) {
6283
var bbox = [
6384
'5.5419158935546875',

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ module.exports.prototype.customNear = function(field) {
6969
return function(query, point) {
7070
point = point.split(',');
7171

72-
if (point.length === 2) {
72+
if (point.length >= 2) {
7373
point[0] = parseFloat(point[0], 10);
7474
point[1] = parseFloat(point[1], 10);
7575

7676
if (!isNaN(point.reduce(function(a,b){return a+b;}))) {
77+
var max = parseInt(point[2], 10);
78+
var min = parseInt(point[3], 10);
79+
7780
query[field] = {
7881
$near: {
7982
$geometry: {
@@ -82,6 +85,14 @@ module.exports.prototype.customNear = function(field) {
8285
},
8386
},
8487
};
88+
89+
if (!isNaN(max)) {
90+
query[field].$near.$maxDistance = parseInt(point[2], 10);
91+
92+
if (!isNaN(min)) {
93+
query[field].$near.$minDistance = parseInt(point[3], 10);
94+
}
95+
}
8596
}
8697
}
8798
};

0 commit comments

Comments
 (0)