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:该索引还支持以下查询运算符和命令,用于使用球面几何图形计算距离:
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
索引。
$nearSphere
$centerSphere
$near
$geoNear
pipeline stage with the spherical: true
option$geoNear
管道阶段,使用spherical: true
选项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:要转换:
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 ] } } } )
If specifying latitude and longitude coordinates, list the longitude first and then latitude:如果指定纬度和经度坐标,请先列出经度,然后列出纬度:
-180
and 180
, both inclusive.-180
和`之间(包括180和180)。-90
and 90
, both inclusive.-90
和90
之间(包括这两个值)。