文档菜单
文档首页
/
MongoDB 手册
/ / / /

$centerSphere

本页内容

  • 定义
  • 行为
  • 示例
$centerSphere

定义一个圆,用于地理空间查询,该查询使用球面几何。查询返回位于圆内的文档。您可以在$centerSphere操作符上同时使用GeoJSON对象和旧坐标对。

要使用$centerSphere,指定一个数组,该数组包含

  • 圆心点的网格坐标,以及

  • 以弧度为单位的圆的半径。要计算弧度,请参阅将距离转换为球面操作符的弧度。

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

重要

如果您使用经纬度,请先指定经度。

应用程序可以使用 $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 ] } }
} )

返回

$center

本页内容