$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您可以在GeoJSON对象和旧坐标对上使用$centerSphere
operator on both GeoJSON objects and legacy coordinate pairs.$centerSphere
运算符。To use要使用$centerSphere
, specify an array that contains:$centerSphere
,请指定一个包含以下内容的数组:The grid coordinates of the circle's center point, and圆中心点的栅格坐标,以及The circle's radius measured in radians.圆的半径以弧度为单位测量。To calculate radians, see Convert Distance to Radians for Spherical Operators.若要计算弧度,请参阅将距离转换为球面运算符的弧度。
{
<location field>: {
$geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] }
}
}ImportantIf 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
.2dsphere
和2d
地理空间索引都支持$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 ] } }
} )