问题描述
我有一些商店的藏书,这些藏书在位置属性上具有地理空间索引.
I have a collection of stores with a geospacial index on the location propery.
在给定用户的纬度,纬度和搜索半径(mi)的情况下,我想做的是,我想返回这些参数内的商店列表.
What I am trying to do is given the user's latitude, latitude and a search radius (mi), I want to return the list of stores that are within those parameters.
我在MongoDB文档上看到了以下示例( http://www.mongodb. org/display/DOCS/Geospatial + Indexing ),但距离似乎以弧度为单位.
I saw the following example on the MongoDB documentation (http://www.mongodb.org/display/DOCS/Geospatial+Indexing), but it looks like the distance is in radians.
center = [50, 50]
radius = 10
db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})
那么,将英里转换为弧度的公式是什么?
So, what is the formula to convert miles to radians?
解决方案mongodb-user上的出色人员帮助我找到了答案.基本上,我真正想要的是一种限制距离的方法.
SolutionThe awesome people at mongodb-user helped me find the answer.Basically, what I was really looking for was a way to limit the distance.
每个更新后的文档,$ near查询中都有第三个参数:您还可以将$ near使用最大距离
Per, the updated documentation, there is a 3rd parameter to the $near query:You can also use $near with a maximum distance
db.places.find( { loc : { $near : [50,50] , $maxDistance : 5 } } ).limit(20)
$ maxDistance的值以弧度为单位;因此,我不得不将自己的距离以英里为单位,再除以69.
The value for $maxDistance is in radians; so, I had to take my distance in miles and divide it by 69.
谢谢!
推荐答案
如文档所述:
所有距离均使用弧度.这使您可以轻松乘以地球半径(大约6371公里或3959英里),以您选择的单位获得距离.相反,进行查询时除以地球半径.
All distances use radians. This allows you to easily multiply by the radius of the earth (about 6371 km or 3959 miles) to get the distance in your choice of units. Conversely, divide by the radius of the earth when doing queries.
因此,您只需要将半径除以地球半径即可.在您的示例中,它将是:
so you simple need to divide your radius by the earth radius.in your example it would be:
radius = 10/3959
这篇关于MongoDB绑定查询:如何将英里转换为弧度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!