MongoDB地理空间索引介绍
-
地理空间索引分为两类:2d索引和2d sphere索引。
2d索引
2d索引可以用来存储和查找平面上的点,一般用于游戏地图中。 以下是部门语法:
向集合中插入一条记录点的数据:
db.my_collect.insert({x:[90,90]})
插入数据的格式为[经度,纬度],取值范围,经度[-180,180],纬度[-90,90]。数据插入成功之后,我们先通过如下命令创建索引:
db.my_collect.ensureIndex({x:"2d"})
通过$near我们可以查询某一个点附近的点,如下:
db.my_collect.find({x:{$near:[90,90]}})
默认情况下返回该点附近100个点,可以通过$maxDistance来设置返回的最远距离:
db.my_collect.find({x:{$near:[90,90],$maxDistance:99}})
也可以通过$geoWithin查询某个形状内的点,比如查询矩形中的点:
db.my_collect.find({x:{$geoWithin:{$box:[[0,0],[90,1]]}}})
两个坐标点用来确定矩形的位置。
查询圆中的点:
db.my_collect.find({x:{$geoWithin:{$center:[[0,0],45]}}})
参数分别表示圆的圆心和半径。
查询多边形中的点:
db.my_collect.find({x:{$geoWithin:{$polygon:[[0,0],[100,0],[100,1],[0,1]]}}})
这里可以填入任意多个点,表示多边形中的各个点。
2d sphere索引
2dsphere可以用来存储和查找球面上的点,适用于球面类型的地图,它的数据类型是GeoJSON格式的,比如我们描述一个点,GeoJSON如下:
{ "_id" : ObjectId("59f5e0571f9e8e181ffc3196"), "name" : "shenzhen", "location" : { "type" : "Point", "coordinates" : [ 90.0, 0.0 ] } }
描述线,GeoJSON格式如下:
{ "_id" : ObjectId("59f5e0d01f9e8e181ffc3199"), "name" : "shenzhen", "location" : { "type" : "LineString", "coordinates" : [ [ 90.0, 0.0 ], [ 90.0, 1.0 ], [ 90.0, 2.0 ] ] } }
描述多边形,GeoJSON格式如下:
{ "_id" : ObjectId("59f5e3f91f9e8e181ffc31d0"), "name" : "beijing", "location" : { "type" : "Polygon", "coordinates" : [ [ [ 0.0, 1.0 ], [ 0.0, 2.0 ], [ 1.0, 2.0 ], [ 0.0, 1.0 ] ] ] } }
还有其他的类型。有了数据之后,我们可以通过如下操作来创建地理空间索引了:
db.my_collect.ensureIndex({location:"2dsphere"})
比如我想查询和深圳这个区域有交集的文档,如下:
var shenzhen = db.sang_collect.findOne({name:"shenzhen"}) db.my_collect.find({location:{$geoIntersects:{$geometry:shenzhen.location}}})
这里的查询结果是和深圳这个区域有交集的都会查到(比如经过深圳的高速公路、铁路等),我们也可以只查询深圳市内的区域(比如深圳市内所有的学校),如下:
var shenzhen = db.sang_collect.findOne({name:"shenzhen"}) db.my_collect.find({location:{$within:{$geometry:shenzhen.location}}})
也可以查询腾讯附近的其他位置,如下:
var QQ = db.sang_collect.findOne({name:"QQ"}) db.my_collect.find({location:{$near:{$geometry:QQ.location}}})
复合地理空间索引
位置往往只是我们查询的一个条件,比如我要查询深圳市内所有的学校,那我得再增加一个查询条件,如下:
var shenzhen = db.sang_collect.findOne({name:"shenzhen"}) db.my_collect.find({location:{$within:{$geometry:shenzhen.location}},name:"QQ"})