Calculate Distance Using Spherical Geometry使用球面几何图形计算距离

Warning警告

For spherical queries, use the 2dsphere index result.对于球形查询,请使用2dsphere索引结果。

The use of 2d index for spherical queries may lead to incorrect results, such as the use of the 2d index for spherical queries that wrap around the poles.对球形查询使用2d索引可能会导致不正确的结果,例如对环绕极点的球形查询使用2d索引。

The 2d index supports queries that calculate distances on a Euclidean plane (flat surface). 2d索引支持计算欧几里得平面(平面)上距离的查询。The index also supports the following query operators and command that calculate distances using spherical geometry:该索引还支持以下查询运算符和命令,用于使用球面几何图形计算距离:

Note注意

While basic queries using spherical distance are supported by the 2d index, consider moving to a 2dsphere index if your data is primarily longitude and latitude.虽然2d索引支持使用球面距离的基本查询,但如果您的数据主要是经度和纬度,请考虑移动到2dsphere索引。

Important重要

The aforementioned operations use radians for distance. 上述操作使用弧度表示距离。Other spherical query operators do not, such as $geoWithin.其他球形查询运算符则不然,例如$geoWithin

For spherical query operators to function properly, you must convert distances to radians, and convert from radians to the distances units used by your application.要使球形查询运算符正常工作,必须将距离转换为弧度,并将弧度转换为应用程序使用的距离单位。

To convert:要转换:

  • distance to radians弧度距离: divide the distance by the radius of the sphere (e.g. the Earth) in the same units as the distance measurement.:将距离除以球体半径(如地球),单位与距离测量单位相同。
  • radians to distance弧度到距离: multiply the radian measure by the radius of the sphere (e.g. the Earth) in the units system that you want to convert the distance to.:将弧度度量乘以要将距离转换为的单位制中球体(例如地球)的半径。

The equatorial radius of the Earth is approximately 3,963.2 miles or 6,378.1 kilometers.地球赤道半径约为3963.2英里或6378.1公里。

The following query would return documents from the places collection within the circle described by the center [ -74, 40.74 ] with a radius of 100 miles:以下查询将从半径为100英里的圆心[-74, 40.74]所描述的圆圈内的places集合返回文档:

db.places.find( { loc: { $geoWithin: { $centerSphere: [ [ -74, 40.74 ] ,
                                                     100 / 3963.2 ] } } } )
Note注意

If specifying latitude and longitude coordinates, list the longitude first and then latitude:如果指定纬度和经度坐标,请先列出经度,然后列出纬度

  • Valid longitude values are between -180 and 180, both inclusive.有效的经度值介于-180和`之间(包括180和180)。
  • Valid latitude values are between -90 and 90, both inclusive.有效的纬度值介于-9090之间(包括这两个值)。
←  2d Index InternalsgeoHaystack Indexes →