dongmi1221 2014-11-03 08:20
浏览 377
已采纳

如何在mongodb中找到经纬度附近的地方?

Am very new to mongodb and golang. I have a collection named "myplace" It has the following fileds place_name, city, latitude, longitude. My question is user in some place and searching the nearby places. How can I query to mongodb to find near by locations. Also in golang.

My doc structure

{
    "_id" : ObjectId("544a2147785b707b340ed6c7"),
    "latitude" : 12.36547,
    "longitude" : 1.235689,
    "place_name" : "some_place",
    "city" : "Some city"
}

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongwang6837 2014-11-03 09:30
    关注

    Hi For your case I think you should changed above doc as below

        {
        "_id" : ObjectId("545749dba2b0b4cf603a7546"),
        "city" : "B",
        "placeName" : "A",
        "loc" : {
            "lon" : 51.10682735591432,
            "lat" : -114.11773681640625
        }
    }
    {
        "_id" : ObjectId("545749f3a2b0b4cf603a7547"),
        "city" : "B1",
        "placeName" : "A1",
        "loc" : {
            "lon" : 51.09144802136697,
            "lat" : -114.11773681640625
        }
    }
    

    After that indexing the above documents as below

    db.collectionName.ensureIndex({loc:"2d"})
    

    If indexing executing properly then write following query to find out near by documents

    db.location.find({loc: {$near:[51,-114]}})
    

    for more help you should refer this mongo $near and $geoNear click here

    and sorry for golang because I don't know more about golang

    for golang

    var places []Place
    lat := 51.515614
    long := -0.268998
    err = coll.Find(bson.M{"loc": bson.M{"$near": []float64{long, lat}, "$maxDistance" :      0.056}}).All(&places)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?