OPTIONS
翻译或纠错本页面

查询 2dsphere 索引

接下来的部分将会描述 2dsphere 索引所支持的查询。您可以参见 Geospatial Query Compatibility 对推荐使用的地理查询有个全面了解。

查找在某个多边形内部的GeoJSON对象

操作符 $geoWithin 可以在一个GeoJSON多边形内部查找位置信息。位置信息必须以GeoJSON格式存储。使用如下格式:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                           { $geometry :
                             { type : "Polygon" ,
                               coordinates : [ <coordinates> ]
                      } } } } )

下例可以得到完全处于一个GeoJSON多边形区域内的所有点和形状:

db.places.find( { loc :
                  { $geoWithin :
                    { $geometry :
                      { type : "Polygon" ,
                        coordinates : [ [
                                          [ 0 , 0 ] ,
                                          [ 3 , 6 ] ,
                                          [ 6 , 1 ] ,
                                          [ 0 , 0 ]
                                        ] ]
                } } } } )

GeoJSON对象的交叉

2.4 新版功能.

操作符 $geoIntersects 可以查找和某个指定GeoJSON对象相交的位置。如果一个位置和一个GeoJSON对象的交集是非空的,那么它们是相交的。这也包括了共享边的情况。

操作符 $geoIntersects 格式如下:

db.<collection>.find( { <location field> :
                         { $geoIntersects :
                           { $geometry :
                             { type : "<GeoJSON object type>" ,
                               coordinates : [ <coordinates> ]
                      } } } } )

下例使用 $geoIntersects 来选取和多边形相交的所有被索引的点和形状,多边形由 coordinates 数组定义:

db.places.find( { loc :
                  { $geoIntersects :
                    { $geometry :
                      { type : "Polygon" ,
                        coordinates: [ [
                                         [ 0 , 0 ] ,
                                         [ 3 , 6 ] ,
                                         [ 6 , 1 ] ,
                                         [ 0 , 0 ]
                                       ] ]
                } } } } )

和GeoJSON点邻近

邻近查询可以返回离指定点最近的点并按距离排序。在GeoJSON数据上使用邻近查询时,需要有 2dsphere 索引。

可以使用 $near 操作符或者 geoNear 命令来查询某个GeoJSON点的邻近点。距离以米为单位。

操作符 $near 格式如下:

db.<collection>.find( { <location field> :
                         { $near :
                           { $geometry :
                              { type : "Point" ,
                                coordinates : [ <longitude> , <latitude> ] } ,
                             $maxDistance : <distance in meters>
                      } } } )

参见 $near 了解更多例子。

命令 geoNear 的格式如下:

db.runCommand( { geoNear : <collection> ,
              near : { type : "Point" ,
                       coordinates: [ <longitude>, <latitude> ] } ,
              spherical : true } )

命令 geoNear$near 相比,可以有更多的选项,且返回更多的信息。 geoNear 查看命令详情。

在一个球面上,查找圆内部的点

如果需要在球面上选取位于 “球冠(spherical cap)” 内部的所有网格坐标,可以使用 $geoWithin$centerSphere 操作符。需要指定一个包含如下元素的数组:

格式如下:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                           { $centerSphere :
                              [ [ <x>, <y> ] , <radius> ] }
                      } } )

如下示例会查询所有网格坐标并返回处于圆内的文档,这个圆以经度 88W 纬度 30N 为圆心,半径 10英里。这个例子将距离10英里转换成了弧度,通过将距离除以地球近似半径3959英里的方式:

db.places.find( { loc :
                  { $geoWithin :
                    { $centerSphere :
                       [ [ -88 , 30 ] , 10 / 3959 ]
                } } } )