您好,对于您的情况,我认为您应该更改上面的文档,如下所示
{
"_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]}})
如需更多帮助,您应该参考此 mongo $near 和 $geoNear点击这里 http://docs.mongodb.org/manual/reference/operator/query/near/
对 golang 感到抱歉,因为我不太了解 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)