LBS(Location Based Services)定位服务,即根据用户位置查询用户附近相关信息,这一功能在很多应用上都有所使用。基于用户位置进行查询时,需要提供用户位置的经纬度。为了提高查询速度,MongoDB为坐标平面查询提供了专门的索引,称作地理空间(2d)索引。
地理空间索引又称为2d
索引。创建其它形式的索引,我们会按升序或降序(1
或-1
)的形式创建索引,不同于其它形式的索引,创建地理空间索引要指定的值为:2d
。语法结构如下:
db.<collection>.createIndex({ <location field> : "2d" , <additional field> : <value> } , { <index-specification options>})
location field
:要创建2d
地理空间索引的字段(键)
additional field
:附加字段(键)
index-specification options
:索引选项
index-specification options
是一个包含以下可选值的子文档:
{ min : <lower bound>, max : <upper bound>, bits : <bit precision> }
min bound
:{number},最低范围,默认-180.0
min bound
:{number},最高范围,默认180.0
bit precision
:{integer},存储数据Geohash值精度,取值:1〜32,默认26
地理空间计算本质上是二维数据计算,创建索引地理空间索引时,索引键的值必须是一对值:一个包含两个数值的数组或包含两个键的内嵌文档(内嵌文档键的名称不重要)。
以下几种健值形式,都可以创建地理空间索引:
// 数组{"gps": [40, 120]}// 包含两个键的内嵌文档{"gps": { "x":40, "y":120}}{"gps": { "latitude":40, "longitude":120}}
我们可以对上面的"gps"
健创建地理空间索引:
db.userlocation.ensureIndex({"gps" : "2d"}, {"min":-1000, "max":1000});
这样我们就创建了地理空间索引值范围为-1000〜1000
的索引。
$near
接近点查询通过$near
关键字,可以根据一个指定的平面点,按距离排序返回查询结果:
db.<collection>.find({ <location field> :{ $near : [ <x>, <y>], $maxDistance : <distance in meters<, $mixDistance : <distance in meters< }})
$near
表示要查询的中心点
$maxDistance
距中心点的最大距离
$mixDistance
距中心点的最小距离
如,查询距离坐标点(40,120),10公里以内的数据:
db.userlocation.find({ gps : { $near : [40, 120], $maxDistance : 10 }})
$geoWithin
指定形状查询MongoDB不仅可以按坐标点查询,还可以在查询指定形状内的文档。按形状查询使用$geoWithin
(在v2.4
之前使用$within
):
db.<collection<.find({ <location field> :{ $geoWithin : { $box|$polygon|$center : <coordinates>} }})
在指定形状查询中,$box
、$polygon
、$center
分别表示按矩形、五边形、圆形进行查询。
如,查询坐标点为(40,120),半径为10以内的文档:
db.userlocation.find({ gps : { $geoWithin : { $center:[[40, 120], 10] } }})
联系客服