1

Mongodb查询 2dsphere 索引

Posted by 撒得一地 on 2016年4月19日 in Mongodb教程

以下内容将介绍 2dsphere 索引支持的查询操作。

多边形内部的 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 对象特定的相交的位置。如果对象位置交集不为空,说明包含了一个共享的边缘文档。

$geoIntersects 运算符使用以下语法 ︰

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

下面的示例使用 $geoIntersects 操作符来输出所有被索引的点和用坐标数组定义的多边形相交的形状。

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

GeoJSON 临近点查询

临近度查询返回了离被定义点距离最近的点,并按距离大小进行结果排序。GeoJSON 数据的临近查询需要 2dsphere 索引。

要查询 GeoJSON 临近点,可以使用 $near 运算符或 geoNear 命令。距离是以米做单位。

$near 操作符使用了以下语法

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

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

下面的示例查询网格坐标并以 88 W 经度 和  30 N 纬度为圆心,然后返回圆内所有在 10 英里半径范围内的文档,。该示例将距离 10 英里,转换成弧度,然后将距离除以地球近似3963.2 英里的赤道半径︰

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

上一篇:

下一篇:已是最新文章

相关推荐

1 Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注

9 + 1 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.