I want to query against a geospatial index in mongo-db (designed after this tutorial http://www.mongodb.org/display/DOCS/Geospatial+Indexing).

So when I execute this from the shell everything works fine:

db.sellingpoints.find(( { location : { $near: [48.190120, 16.270895], $maxDistance: 7 / 111.2 } } );

but the same query from my nodejs application (using mongoskin or mongoose), won't return any results until i set the distance-value to a very high number (5690)

db.collection('sellingpoints')
 .find({ location: { $near: [lat,lng], $maxDistance: distance / 111.2} })
 .limit(limit)
 .toArray(callback);

Has someone any idea how to fix that?

有帮助吗?

解决方案

I just tried a simple node mongoose script against some geo localized data. I'm using the cities geo data from http://dev.maxmind.com/geoip/geolite, loading it all into mongodb. I've wrote a small test code for this (https://github.com/nleite/mongoose_test).

var mongoose = require('mongoose');
var db = mongoose.createConnection( 'localhost', 'geo');

var schema = mongoose.Schema( { city: 'string', loc: 'array', areaCode: 'string', country:      'string', region: 'string', locId: 'string', metroCode: 'string', postalCode: 'string'} );

var City = db.model( 'City', schema);
var distance = 7 / 111.2;
console.log( 'Distance %s', distance);
var query = City.find( { 'loc':  { $near: [48.190120, 16.270895], $maxDistance: distance      }} ); 
query.exec( function( err, city){
   if (err) return handleError(err);
   console.log( 'Found you %s' , city);
   process.exit(-1);
}); 

It seems to be working pretty well. I've even took your query to check if there could be any problem with it, but seems to be working fine.

Can you please give as more info on the Schema and the Query that is not working as you expected ?

其他提示

All mongo geo query should be in format of (long,lat) and NOT (lat,long). Can you try with that format.

PS : you need to ensure data is inserted in (long,lat) format as well as query in also (long,lat) format

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top