MongoDB 绑定查询:如何将英里转换为弧度?

发布于 2024-09-26 10:21:05 字数 728 浏览 0 评论 0原文

我有一个在位置属性上具有地理空间索引的商店集合。

我想做的是给定用户的纬度、纬度和搜索半径(mi),我想返回这些参数内的商店列表。

我在 MongoDB 文档中看到以下示例(http://www.mongodb.org/display /DOCS/Geospatial+Indexing),但看起来距离以弧度为单位。

center = [50, 50]
radius = 10
db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})

那么,将英里转换为弧度的公式是什么?

解决方案 mongodb-user 的优秀人员帮助我找到了答案。 基本上,我真正寻找的是一种限制距离的方法。

根据更新的文档,$near 查询有第三个参数: 您还可以将 $near 与最大距离一起使用。

db.places.find( { loc : { $near : [50,50] , $maxDistance : 5 } } ).limit(20)

$maxDistance 的值以弧度为单位;所以,我必须将距离(以英里为单位)除以 69。

谢谢!

I have a collection of stores with a geospacial index on the location propery.

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.

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?

Solution
The awesome people at mongodb-user helped me find the answer.
Basically, what I was really looking for was a way to limit the distance.

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)

The value for $maxDistance is in radians; so, I had to take my distance in miles and divide it by 69.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖正好 2024-10-03 10:21:05

正如文档所说:

所有距离都使用弧度。这使您可以轻松地乘以地球半径(约 6371 公里或 3959 英里),以获得您选择的单位的距离。相反,查询时除以地球半径。

所以你只需要将你的半径除以地球半径。
在你的例子中它将是:

radius = 10/3959

As docs say:

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
遗心遗梦遗幸福 2024-10-03 10:21:05

为了使用 mongodb $near 查询与 km 边界,您需要将半径值转换为 km。默认情况下,mongodb $near 接受 $maxDistance 作为半径。

使用 km 时,将距离转换为 111.12(1 度大约为 111.12 公里),或者保留使用 Degree let 时的距离,

如果您的半径以 km 为单位,则使用,

 db.places.find( { loc : { $near : [50,50] , $maxDistance : 10/111.12 } } )

但如果您的半径以英里为单位,则将其转换为 km

In order to use mongodb $near queries with km bounds, you need to convert the radius value to km. By default mongodb $near accepts $maxDistance as radius.

Convert distance by 111.12 (one degree is approximately 111.12 kilometers) when using km, or leave distance as it is on using degree

let if your radius is in km then use

 db.places.find( { loc : { $near : [50,50] , $maxDistance : 10/111.12 } } )

but if your radius in miles then convert it in km

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文