[英]How to execute runCommand with Mongoose?
I am using Node.js and Mongoose to access my MongoDB.我正在使用 Node.js 和 Mongoose 来访问我的 MongoDB。 I am using a model that stores some geo coordinates.
我正在使用存储一些地理坐标的 model。 I have them indexed and everything seems to work as expected.
我将它们编入索引,一切似乎都按预期工作。 What I am trying to do is to retrieve the most near things from my request.
我想做的是从我的请求中检索最接近的东西。 On the MongoDB console I do something like this:
在 MongoDB 控制台上,我执行以下操作:
distances = db.runCommand({ geoNear : "deals", near : [11.252, 14.141], spherical : true, maxDistance : 300 }).results
However, I am not sure how to do this with Mongoose.但是,我不确定如何使用 Mongoose 执行此操作。 Here is more information about the command I am trying to use: http://www.mongodb.org/display/DOCS/Geospatial+Indexing
以下是有关我尝试使用的命令的更多信息: http://www.mongodb.org/display/DOCS/Geospatial+Indexing
Thanks, Jose谢谢,何塞
First of all there is not yet a convenience wrapper to use geoNear with Mongoose directly (given that you want to read the calculated distance).首先,还没有一个方便的包装器可以直接将 geoNear 与 Mongoose 一起使用(假设您想读取计算的距离)。
But since Mongoose collections proxies all collection methods from the native MongoDB native driver you can just use their geoNear method , though you have to give up a little bit of convenience you might expect from Mongoose and in my findings the error handling was a little bit different. But since Mongoose collections proxies all collection methods from the native MongoDB native driver you can just use their geoNear method , though you have to give up a little bit of convenience you might expect from Mongoose and in my findings the error handling was a little bit different .
Anyway, this is how you could use said API:无论如何,这就是你可以使用 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;
}
});
Please refer to the docs for correct error handling and how to calculate the distances .请参阅文档以了解正确的错误处理以及如何计算距离。
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 version v2.0.2节点 0.6.6,猫鼬@2.4.9,mongodb 版本 v2.0.2
its a bit hacky and could change.它有点hacky,可能会改变。
Hope this should work:!希望这应该工作:! Referrence URL : http://mongoosejs.com/docs/api.html
参考 URL : http://mongoosejs.com/docs/api.html
// Legacy point // 遗留点
Model.geoNear([1,3], { maxDistance : 5, spherical : true }, function(err, results, stats) {
console.log(results);
});
// geoJson //geoJson
var point = { type : "Point", coordinates : [9,9] };
Model.geoNear(point, { maxDistance : 5, spherical : true }, function(err, results, stats) {
console.log(results);
});
I don't think Mongoose supports runCommand
right now.我不认为 Mongoose 现在支持
runCommand
。 You would be better off using the geospatial options using the find
method eg >您最好使用
find
方法使用地理空间选项,例如 >
db.places.find( { location : { $near : [50,50] , $maxDistance : 300 } } )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.