I have read the documentation here talks about writing query to get some location within the radius:
db.restaurants.find({ location:
{ $geoWithin:
{ $centerSphere: [ [ -73.93414657, 40.82302903 ], 5 / 3963.2 ] } } })
Now I try to write it using mgo
driver but I don't get the idea how to write it here what I have tried :
var cites []City
collection := mongo.DB("Db").C("Collection")
err = collection.Find(bson.M{
"location": bson.M{
"$geoWithin": bson.M{
"$centerSphere" : [ [ -73.93414657, 40.82302903 ], 5 / 3963.2 ],
},
},
}).All(&cites)
Yes above code absolutely not working becuase I don't know how to translate this [ [ -73.93414657, 40.82302903 ], 5 / 3963.2 ]
in go?
For $centerSphere
you have to pass a center point and a radius in a slice of type []interface{}
, where the point is also a slice containing its coordinates, can also be of type []interface{}
.
err = collection.Find(bson.M{
"location": bson.M{
"$geoWithin": bson.M{
"$centerSphere": []interface{}{
[]interface{}{-73.93414657, 40.82302903}, 5 / 3963.2,
},
},
},
}).All(&cites)
See a related / possible duplicate question: