我正在使用Node.js和Mongoose访问我的MongoDB。我正在使用一个存储一些地理坐标的模型。我已经将它们编入索引,一切似乎都像预期的那样工作。我正在尝试做的是从我的请求中检索最接近的东西。在MongoDB控制台上,我这样做:
distances = db.runCommand({ geoNear : "deals", near : [11.252, 14.141], spherical : true, maxDistance : 300 }).results 然而,我不确定如何在Mongoose中做到这一点。下面是关于我尝试使用的命令的更多信息:http://www.mongodb.org/display/DOCS/Geospatial+Indexing
谢谢你,何塞
发布于 2012-08-06 16:52:53
首先,还没有一个方便的包装器来直接在Mongoose中使用geoNear (假设您想要读取计算出的距离)。
但是因为Mongoose从原生MongoDB native driver收集proxies all collection methods,所以你可以只使用他们的geoNear method,尽管你不得不放弃一点你可能期望从Mongoose获得的便利,在我的发现中,错误处理有点不同。
不管怎样,你可以这样使用这个API:
YourModel.collection.geoNear(lon, lat, {spherical: true, maxDistance: d}, function(err, docs) {
if (docs.results.length == 1) {
var distance = docs.results[0].dis;
var match = docs.results[0].obj;
}
});有关正确的错误处理和how to calculate the distances,请参阅文档。
发布于 2012-01-16 21:09:24
YourModel.db.db.executeDbCommand({geoNear : "locations", near : [11.252,14.141], spherical: true }, function(err,res) { console.log(res.documents[0].results)});node 0.6.6,mongoose@2.4.9,mongodb版本v2.0.2
这有点老生常谈,可能会改变。
发布于 2011-05-03 03:02:31
我不认为Mongoose现在支持runCommand。最好使用使用find方法的地理空间选项,例如>
db.places.find( { location : { $near : [50,50] , $maxDistance : 300 } } )https://stackoverflow.com/questions/5861134
复制相似问题