Skip to content

Commit

Permalink
Added support for GeoJSON on Query#near
Browse files Browse the repository at this point in the history
Had to upgrade to mquery 0.2.7 to do that. Also added tests for this.
fixes #1482
  • Loading branch information
ebensing authored and aheckmann committed Aug 21, 2013
1 parent 41868b3 commit 0764330
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2651,7 +2651,7 @@ Query.prototype.near = function () {
params.push({ center : arguments[0], spherical : sphere });
} else if ('string' == typeof arguments[0]) {
params.push(arguments[0]);
} else if (arguments[0].constructor.name == 'Object') {
} else if (utils.isObject(arguments[0])) {
params.push(arguments[0]);
}
} else if (arguments.length === 2) {
Expand All @@ -2660,7 +2660,7 @@ Query.prototype.near = function () {
} else if ('string' == typeof arguments[0] && Array.isArray(arguments[1])) {
params.push(arguments[0]);
params.push({ center : arguments[1], spherical : sphere });
} else if ('string' == typeof arguments[0] && arguments[1].constructor.name == 'Object') {
} else if ('string' == typeof arguments[0] && utils.isObject(arguments[1])) {
params.push(arguments[0]);
params.push(arguments[1]);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
, "mpromise": "0.3.0"
, "mpath": "0.1.1"
, "regexp-clone": "0.0.1"
, "mquery" : "0.2.6"
, "mquery" : "0.2.7"
}
, "devDependencies": {
"mocha": "1.12.0"
Expand Down
31 changes: 31 additions & 0 deletions test/model.querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,37 @@ describe('geo-spatial', function(){
}
});

it('$near works with GeoJSON (gh-1482)', function (done) {
var geoJSONSchema = new Schema({ loc : { type : { type : String }, coordinates : [Number] } });
geoJSONSchema.index({ loc : '2dsphere' });
var name = 'geospatial'+random();
var db = start()
, Test = db.model('Geo1', geoJSONSchema, name);

var pending = 2;
function complete (err) {
if (complete.ran) return;
if (err) return done(complete.ran = err);
--pending || test();
}

Test.on('index', complete);
Test.create({ loc: { type : 'Point', coordinates :[ 10, 20 ]}}, { loc: {
type : 'Point', coordinates: [ 40, 90 ]}}, complete);

function test () {
// $maxDistance is in meters... so even though they aren't that far off
// in lat/long, need an incredibly high number here
Test.where('loc').near({ center : { type : 'Point', coordinates :
[11,20]}, maxDistance : 1000000 }).exec(function (err, docs) {
db.close();
assert.ifError(err);
assert.equal(1, docs.length);
done();
});
}
});

it('$within arrays (gh-586)', function(done){
var db = start()
, Test = db.model('Geo2', geoSchema, collection + 'geospatial');
Expand Down
12 changes: 12 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,18 @@ describe('Query', function(){
assert.deepEqual(query._conditions, {checkin: {$near: [40, -72]}});
done();
})
it('via where, where GeoJSON param', function(done){
var query = new Query({}, {}, null, p1.collection);
query.where('loc').near({ center : { type : 'Point', coordinates : [40, -72 ]}});
assert.deepEqual(query._conditions, {loc: {$near: { $geometry : { type : 'Point', coordinates : [40, -72] }}}});
done();
})
it('not via where, where GeoJSON param', function(done){
var query = new Query({}, {}, null, p1.collection);
query.near('loc', { center : { type : 'Point', coordinates : [40, -72 ]}});
assert.deepEqual(query._conditions, {loc: {$near: { $geometry : { type : 'Point', coordinates : [40, -72] }}}});
done();
})
})

describe('nearSphere', function(){
Expand Down

0 comments on commit 0764330

Please sign in to comment.