MongoDB 坐标_距离

创建集合

db.testGps.insert(
{
    "_id" : NumberLong("908531"),
    "_class" : "com.xxxxx",
    "loc" : {
        "type" : "Point",
        "coordinates" : [
            121.4624,
            31.2262
        ]
    }
});

创建2dsphere索引

db.testGps.createIndex( 
      {"loc" : "2dsphere" }
);

第一种

// 返回单位为 米
db.testGps.aggregate([
    {
     $geoNear: {
        near: { type: "Point", coordinates: [121.462,31.226 ] },//coordinates: [0,0] 经度,维度
        distanceField: "dis.calculated",
        // maxDistance: 50000,             //指定最大距离,不指定则为最大值
        query: { "_id":NumberLong("908531") }, //查询条件
        // includeLocs: "dist.location",
        // num: 5,  //返回的行数 ,不写:默认返回所有
        spherical: true, //2dsphere 必须指定
        // distanceMultiplier:63781 // 不需要
     }
}])

第二种

//若需要距离单位为  米  则指定  // distanceMultiplier: 6378137 
//若需要距离单位为 千米 则指定  // distanceMultiplier: 6378 
db.runCommand({
    "geoNear":"testGps",
    "near":[121.462,31.226 ] ,
    // "num":10, 
    spherical:true,
    distanceMultiplier: 6378137,  
    maxDistance:10, 
     distance:"dis"   //距离别名
    })

参考:https://blog.youkuaiyun.com/forandever/article/details/44751801
参考:http://makaidong.com/xiaoyu411502/1/194475_9677400_2.htm
参考:https://docs.mongodb.org/manual/core/2dsphere/
参考:https://docs.mongodb.org/manual/reference/geojson/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值