MongoDB-5-地理位置计算

版权声明:本文为博主原创文章,未经博主允许不得转载

MongoDB在使用距离查询时,存储的经纬度结构要类似这样才可以:

'point' : [
    116.296616,
    40.150002
]

#或者:

'point' : {
    'lng' : 116.296616,
    'lat' : 40.150002
}
``

然后给经纬度的point做一个2dSphere索引,告诉mongodb这个是存坐标的

db.shop_list.createIndex({"point":"2dsphere"})

指定点的附近的

db.shop_list.find({'point':{$nearSphere: [116.296616,40.150002]}}) 

指定点的附近1000米

db.shop_list.find({point: { $geoWithin: { $centerSphere: [ [116.296616,40.150002 ], 1000/6378137 ] } } }) 

指定点的附近1000米的10个门店,并且有距离计算值

db.runCommand({ geoNear : "shop_list" , near : [116.296616,40.150002], num : 10 , spherical:true, distanceMultiplier: 6378137, maxDistance:1000/6378137})

猜你喜欢

转载自blog.csdn.net/weixin_44914784/article/details/89313853
今日推荐