如何在Mongoose中执行runCommand? [英] How to execute runCommand with Mongoose?

查看:73
本文介绍了如何在Mongoose中执行runCommand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.js和Mongoose访问我的MongoDB.我正在使用一个存储一些地理坐标的模型.我已将它们编入索引,并且一切似乎都按预期工作.我正在尝试做的是从我的请求中检索最接近的东西.在MongoDB控制台上,我执行以下操作:

I am using Node.js and Mongoose to access my MongoDB. I am using a model that stores some geo coordinates. 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:

distances = db.runCommand({ geoNear : "deals", near : [11.252, 14.141], spherical : true, maxDistance : 300  }).results 

但是,我不确定如何用猫鼬来做到这一点.以下是有关我要使用的命令的更多信息: http://www.mongodb. org/display/DOCS/Geospatial + Indexing

However, I am not sure how to do this with Mongoose. Here is more information about the command I am trying to use: http://www.mongodb.org/display/DOCS/Geospatial+Indexing

谢谢, 何塞

推荐答案

首先,还没有方便的包装器可以直接将geoNear与Mongoose结合使用(假设您要读取计算出的距离).

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).

但是,由于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.

无论如何,这就是您可以使用上述API的方式:

Anyway, this is how you could use said 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.

这篇关于如何在Mongoose中执行runCommand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆