Query a 2dsphere Index查询2dsphere索引

On this page本页内容

The following sections describe queries supported by the 2dsphere index.以下部分描述了2dsphere索引支持的查询。

GeoJSON Objects Bounded by a Polygon以多边形为边界的GeoJSON对象

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 ]
                                        ] ]
                } } } } )

Intersections of GeoJSON ObjectsGeoJSON对象的交集

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 to a GeoJSON Point接近GeoJSON点

Proximity queries return the points closest to the defined point and sorts the results by distance. 接近度查询返回最接近定义点的点,并按距离对结果进行排序。A proximity query on GeoJSON data requires a 2dsphere index.GeoJSON数据的邻近查询需要2dsphere索引。

To query for proximity to a GeoJSON point, use either the $near operator. 要查询与GeoJSON点的接近度,请使用$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聚合管道阶段。

Points within a Circle Defined on a Sphere球体上定义的圆内的点

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 ]
                } } } )
←  2dsphere Indexes2d Indexes →