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
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)