Database Manual / Reference / Query Language / Query Predicates / Geospatial

$centerSphere

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. However, geospatial indexes support much faster queries than the unindexed equivalents.应用程序可以在没有地理空间索引的情况下使用$centerSphere。然而,地理空间索引支持的查询速度比未索引的同类索引快得多。

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. The query converts the distance to radians by dividing by the approximate equatorial radius of the earth, 3963.2 miles:以下示例查询网格坐标并返回经度88 W和纬度30 N的10英里半径内的所有文档。该查询通过除以地球的近似赤道半径3963.2英里将距离转换为弧度:

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