有商店的地理位置坐标信息在mongodb保存,能否按离用户的距离由小到大来排序显示?就是用户周边信息按距离排序的功能
mongodb的排序功能较弱,做不了这么复杂的查询。
你的这种使用场景,理论上来说用couchdb的view来做最简单。
据说[geohash]:http://en.wikipedia.org/wiki/Geohash 是这种找附近用的比较多的方案,好像版本号新一点的mongodb有这种空间分析的功能,具体的没仔细看文档。
MongoDB有2D空间索引,做这种应用很方便。https://foursquare.com/ 目前用的就是MongoDB做的LBS服务。
简单如下:
# 插入数据 lng对应经度 lat对应纬度 均为float型
db.collection.insert({location:[lng,lat],title:'test title'})
# 创建2D空间索引
# 2dsphere支持球面检索 2d二维空间搜索
db.collection.ensureIndex({location:'2dsphere'})
# 按照半径搜索 搜索半径内的所有的点 按照由近到远排序
db.collection.find(
{
location:
{
$geoWithin:
{
$centerSphere: [ [lng, lat], 检索半径]
}
}
}
)
# 搜索全部数据 按照由近到远排序
db.collection.find(
{
location:
{
$near:
[lng, lat]
}
}
)
搜索出的结果已经是由远到近排序了,如果需要输出距离计算。$geoNear
搜索返回的数据中已有distance(距离),不过不支持其他命令,如limit
MongoDB官方文档:http://docs.mongodb.org/manual/applications/geospatial-indexes/
相关问题参考:http://.com/q/1010000000345194