您的位置:首页 > 数据库 > Mongodb

MongoDB 地理位置索引

2015-04-05 10:54 507 查看
关于LBS相关项目,一般存储每个地点的经纬度的坐标, 如果要查询附近的场所,则需要建立索引来提升查询效率。 Mongodb专门针对这种查询建立了地理空间索引。 2d和2dsphere索引,分别是针对平面和球面。

{ loc : { lon : 40.739037, lat: 73.992964 } }
一、2d索引

b.location.ensureIndex( { loc : "2d" } )

默认情况下,该索引假定你在索引经度/维度,并且这些值的范围是[-180,180].

如果你在索引其他东西,你可以指定一些选项:

db.location.ensureIndex( { loc : "2d" } , { min : -500 , max : 500 } )

查询:
1.该索引可以用来进行精确查询

db.location.find( { loc : [50,50] } )
2.查询某个点附近的点

db.location.find( { loc : { $near : [50,50] } } )

db.location.find( { loc : { $near : [50,50] } } ).limit(20)
3.还可以对$near增加一个最大距离的参数

db.location.find( { loc : { $near : [50,50] , $maxDistance : 5 } } ).limit(20)

4.MongoDB地理信息索引支持可选的从键。如果你经常对地址和其他属性同时查询,可以增加其他属性到该索引。其他属性作为索引的注解,可以让过滤执行的更快。

db.location.ensureIndex( { location : "2d" , category : 1 } );

db.location.find( { location : { $near : [50,50] }, category : 'coffee' } );
5.MongoDB还是提供了一个执行类似功能的geoNear命令。geoNear命令可以在查询结果中返回每个点距离查询点的距离,也有一些故障诊断信息。

合法的选项有:“near”,"num","maxDistance","distanceMultiplier"和“query”,query可以是任意常规的mongo query。

> db.runCommand( { geoNear : "location" , near : [ 50 , 50 ], num : 10,

... query : { type : "museum" } } );
6.边界查询

可以使用$within代替$near在某个图形内部进行查询。返回的结果并不是按距离进行排序的,在这种无排序的情况下查询会更快一些。支持的图形类型有$box(矩形),$center(圆形),和$polygon(凹和凸的多边形)。所有的边界查询默认包含了图形的边,尽管在浮点类型情形下这一点不能严格依赖。

查询矩形内所有点,你必须指定左下角和右上角的坐标:

> box = [[40.73083, -73.99756], [40.741404, -73.988135]]

> db.places.find({"loc" : {"$within" : {"$box" : box}}})

通过中心点坐标和半径来指定一个圆形:

> center = [50, 50]

> radius = 10

> db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})

通过一个数组或者对象来指定多边形。多边形最后一个点默认会和第一个点相连。

> polygonA = [ [ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ] ]

> polygonB = { a : { x : 10, y : 20 }, b : { x : 15, y : 25 }, c : { x : 20, y : 20 } }

> db.places.find({ "loc" : { "$within" : { "$polygon" : polygonA } } })

> db.places.find({ "loc" : { "$within" : { "$polygon" : polygonB } } })

多边形查询严格限定在多边形内部,目前文档内的多边形不能被Mongodb建立索引。
7.多位置文档

Mongodb还支持对多位置文档建立索引。这些位置可以通过子对象中的数组来指定,例如:

> db.places.insert({ addresses : [ { name : "Home", loc : [55.5, 42.3] }, { name : "Work", loc :

[32.3, 44.2] } ] })

> db.places.ensureIndex({ "addresses.loc" : "2d" })

多位置也可以在单独的字段指定:

> db.places.insert({ lastSeenAt : [ { x : 45.3, y : 32.2 }, [54.2, 32.3], { lon : 44.2, lat : 38.2 } ]

})

> db.places.ensureIndex({ "lastSeenAt" : "2d" })

默认情况下,当在包含多位置文档的集合上执行geoNear或者 $near类型的查询时,相同的文档可能会返回多次。使用$within操作符的查询默认不会返回重复文档。

在V2.0,可以通过对geoNear和$within查询使用$uniqueDocs参数来覆盖默认参数,类似于:

> db.runCommand( { geoNear : "places" , near : [50,50], num : 10, uniqueDocs : false } )

> db.places.find( { loc : { $within : { $center : [[0.5, 0.5], 20], $uniqueDocs : true } } } )

目前不能对$near指定$uniqueDocs参数。

另外,当使用geoNear查询和多位置文档时,在返回距离的同时也返回生成距离的位置信息是很有用的。在v2.0,对geoNear查询指定includeLocs:true

就可以返回位置信息了。返回的位置是文档的位置信息的一份拷贝-如果位置是一个数组,这个对象会有“0”,“1”字段。

> db.runCommand({ geoNear : "places", near : [ 0, 0 ], maxDistance : 20, includeLocs : true }
二、2dsphere索引

db.places.ensureIndex( { loc : "2dsphere"} )
1.$geometry表示查询的几何图片,type表示类型:polygon 多边形

db.places.find( { loc :

                  { $geoWithin :

                    { $geometry :

                      { type : "Polygon",

                        coordinates : [ [

                                          [ 0 , 0 ] ,

                                          [ 3 , 6 ] ,

                                          [ 6 , 1 ] ,

                                          [ 0 , 0 ]

                                        ] ]

                } } } } )
2.查询附近的值

使用$near来查询附近的地点。

db.places.find( { loc :

                        { $near :

                          { $geometry :

                             { type : "Point",

                               coordinates : [ <longitude> , <latitude> ] } ,

                            $maxDistance : <distance inmeters>

                     } } } )
3.查询圆形内的值

查询圆时,需要指定圆心, 半径。

db.places.find( { loc :

                  { $geoWithin :

                    { $centerSphere :

                       [ [ -88 , 30 ] , 10 ]

                } } } )

[-88, 30] 为经纬度, 10为半径。

参考:http://blog.csdn.net/yonggang7/article/details/28109463
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mongodb