定义
$centerSphereDefines 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
$centerSphereoperator on both GeoJSON objects and legacy coordinate pairs.To use
$centerSphere, specify an array that contains:圆中心点的网格坐标,以及
以弧度为单位测量的圆的半径。要计算弧度,请参阅将距离转换为球面弧度操作符。
{ <location field>: { $geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] } } } 重要
如果使用经度和纬度,请先指定经度。
行为
Applications can use $centerSphere without having a geospatial index. However, geospatial indexes support much faster queries than the unindexed equivalents.
Both 2dsphere and 2d geospatial indexes support $centerSphere.
例子
以下示例查询网格坐标并返回经度 88 W 和纬度 30 N 的 10 英里半径内的所有文档。该查询通过除以近似地球赤道半径 3963.2 英里,将距离转换为弧度:
db.places.find( { loc: { $geoWithin: { $centerSphere: [ [ -88, 30 ], 10/3963.2 ] } } } )