$centerSphere

On this page本页内容

Definition定义

$centerSphere

Defines a circle for a geospatial query that uses spherical geometry. 为使用球形几何图形的地理空间查询定义圆。The query returns documents that are within the bounds of the circle. 该查询返回圆圈范围内的文档。You can use the $centerSphere operator on both GeoJSON objects and legacy coordinate pairs.可以在GeoJSON对象和传统坐标对上使用$centerSphere运算符。

To use $centerSphere, specify an array that contains:要使用$centerSphere,请指定包含以下内容的阵列:

{
   <location field>: {
      $geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] }
   }
}

Important重要的

If you use longitude and latitude, specify longitude first.如果使用经度和纬度,请先指定经度。

Behavior行为

Applications can use $centerSphere without having a geospatial index. 应用程序可以使用$centerSphere,而无需地理空间索引。However, geospatial indexes support much faster queries than the unindexed equivalents.然而,地理空间索引支持的查询速度比没有索引的索引要快得多。

Both 2dsphere and 2d geospatial indexes support $centerSphere.2dsphere2d地理空间索引都支持$centerSphere

Example示例

The following example queries grid coordinates and returns all documents within a 10 mile radius of longitude 88 W and latitude 30 N. 下面的示例查询网格坐标,并返回经度88 W、纬度30 N的10英里半径内的所有文档。The query converts the distance to radians by dividing by the approximate equatorial radius of the earth, 3963.2 miles:该查询将距离除以地球的近似赤道半径3963.2英里,转换为弧度:

db.places.find( {
  loc: { $geoWithin: { $centerSphere: [ [ -88, 30 ], 10/3963.2 ] } }
} )