2dsphere Index2dsphere索引On this page本页内容
The following sections describe queries supported by the 以下部分描述了2dsphere index.2dsphere索引支持的查询。
The $geoWithin operator queries for location data found within a GeoJSON polygon. $geoWithin运算符查询在GeoJSON多边形中找到的位置数据。Your location data must be stored in GeoJSON format. 您的位置数据必须以GeoJSON格式存储。Use the following syntax:使用以下语法:
db.<collection>.find( { <location field> :
{ $geoWithin :
{ $geometry :
{ type : "Polygon" ,
coordinates : [ <coordinates> ]
} } } } )
The following example selects all points and shapes that exist entirely within a GeoJSON polygon:以下示例选择完全存在于GeoJSON多边形内的所有点和形状:
db.places.find( { loc :
{ $geoWithin :
{ $geometry :
{ type : "Polygon" ,
coordinates : [ [
[ 0 , 0 ] ,
[ 3 , 6 ] ,
[ 6 , 1 ] ,
[ 0 , 0 ]
] ]
} } } } )
The $geoIntersects operator queries for locations that intersect a specified GeoJSON object. $geoIntersects运算符查询与指定GeoJSON对象相交的位置。A location intersects the object if the intersection is non-empty. 如果交点非空,则位置与对象相交。This includes documents that have a shared edge.这包括具有共享边缘的文档。
The $geoIntersects operator uses the following syntax:$geoIntersects运算符使用以下语法:
db.<collection>.find( { <location field> :
{ $geoIntersects :
{ $geometry :
{ type : "<GeoJSON object type>" ,
coordinates : [ <coordinates> ]
} } } } )
The following example uses 以下示例使用$geoIntersects to select all indexed points and shapes that intersect with the polygon defined by the coordinates array.$geoIntersects选择与coordinates数组定义的多边形相交的所有索引点和形状。
db.places.find( { loc :
{ $geoIntersects :
{ $geometry :
{ type : "Polygon" ,
coordinates: [ [
[ 0 , 0 ] ,
[ 3 , 6 ] ,
[ 6 , 1 ] ,
[ 0 , 0 ]
] ]
} } } } )
Proximity queries return the points closest to the defined point and sorts the results by distance. 接近度查询返回最接近定义点的点,并按距离对结果进行排序。A proximity query on GeoJSON data requires a GeoJSON数据的邻近查询需要2dsphere index.2dsphere索引。
To query for proximity to a GeoJSON point, use either the 要查询与GeoJSON点的接近度,请使用$near operator. $near运算符。Distance is in meters.距离以米为单位。
The $near uses the following syntax:$near使用以下语法:
db.<collection>.find( { <location field> :
{ $near :
{ $geometry :
{ type : "Point" ,
coordinates : [ <longitude> , <latitude> ] } ,
$maxDistance : <distance in meters>
} } } )
For examples, see 有关示例,请参阅$near.$near。
See also the 另请参见$nearSphere operator and the $geoNear aggregation pipeline stage.$nearSphere运算符和$geoNear聚合管道阶段。
To select all grid coordinates in a "spherical cap" on a sphere, use 要选择球体上“球形帽”中的所有栅格坐标,请将$geoWithin with the $centerSphere operator. $geoWithin与$centerSphere运算符一起使用。Specify an array that contains:指定包含以下内容的数组:
Use the following syntax:使用以下语法:
db.<collection>.find( { <location field> :
{ $geoWithin :
{ $centerSphere :
[ [ <x>, <y> ] , <radius> ] }
} } )
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 example converts the distance, 10 miles, to radians by dividing by the approximate equatorial radius of the earth, 3963.2 miles:该示例通过除以地球的近似赤道半径3963.2英里,将距离(10英里)转换为弧度:
db.places.find( { loc :
{ $geoWithin :
{ $centerSphere :
[ [ -88 , 30 ] , 10 / 3963.2 ]
} } } )