How can I find nearby place with latitude and longitude in mongodb?

huangapple go评论66阅读模式
英文:

How can I find nearby place with latitude and longitude in mongodb?

问题

我对MongoDB和Golang非常陌生。我有一个名为"myplace"的集合,它有以下字段:place_name、city、latitude、longitude。我的问题是,用户在某个地方并搜索附近的地点。我如何查询MongoDB以找到附近的位置?还有在Golang中如何实现。

我的文档结构如下:

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

提前感谢。

英文:

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

答案1

得分: 8

你好,根据你的情况,我认为你应该将上面的文档更改如下:

{
    "_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
    }
}

然后按照以下方式对上述文档进行索引:

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

如果索引执行成功,可以使用以下查询语句查找附近的文档:

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

如果需要更多帮助,可以参考MongoDB的$near$geoNear操作符点击这里

对于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)
英文:

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)

答案2

得分: 2

这个链接可能对你有帮助 https://github.com/mendrugory/Airports

MongoDB查询(使用Python)

def get_closest_airports(lonlat, limit=10):
    """
    使用原始查询返回“limit”个最近的机场。

    :param latlon list:
    :return list of airports:
    """
    return Airport.objects(
        __raw__={"loc": {"$near": {"$geometry": {"type": "Point", "coordinates": lonlat}}}}).limit(limit)

JSON结构如下

{
    "city": "Goroka",
    "tz": "Pacific/Port_Moresby",
    "name": "Goroka",
    "dst": "U",
    "loc": {
        "type": "Point",
        "coordinates": [145.391881, -6.081689]
    },
    "country": "Papua New Guinea",
    "iata/faa": "GKA",
    "altitude": 5282.0,
    "icao": "AYGA",
    "timezone": 10.0,
    "id": 1
}
英文:

This link might help you https://github.com/mendrugory/Airports

MongoDB query (python)

def get_closest_airports(lonlat, limit=10):
    """
    Using a raw query it returns the "limit" closest airports.

    :param latlon list:
    :return list of airports:
    """
    return Airport.objects(
        __raw__={"loc": {"$near": {"$geometry": {"type": "Point", "coordinates": lonlat}}}}).limit(limit)

json structure is as following

{"city": "Goroka", "tz": "Pacific/Port_Moresby", "name": "Goroka", "dst": "U", "loc": {"type": "Point", "coordinates": [145.391881, -6.081689]}, "country": "Papua New Guinea", "iata/faa": "GKA", "altitude": 5282.0, "icao": "AYGA", "timezone": 10.0, "id": 1}

huangapple
  • 本文由 发表于 2014年11月3日 16:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/26710271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定