我已阅读here文档,该文档讨论编写查询以获取半径内的某些位置:
db.restaurants.find({ location:
{ $geoWithin:
{ $centerSphere: [ [ -73.93414657, 40.82302903 ], 5 / 3963.2 ] } } })
现在,我尝试使用
mgo
驱动程序编写它,但是我不知道如何在这里尝试编写它: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)
是的,上面的代码绝对不起作用,因为我不知道如何在go中翻译此
[ [ -73.93414657, 40.82302903 ], 5 / 3963.2 ]
? 最佳答案
对于 $centerSphere
,您必须在[]interface{}
类型的 slice 中传递中心点和半径,其中该点也是包含其坐标的 slice ,也可以是[]interface{}
类型。
err = collection.Find(bson.M{
"location": bson.M{
"$geoWithin": bson.M{
"$centerSphere": []interface{}{
[]interface{}{-73.93414657, 40.82302903}, 5 / 3963.2,
},
},
},
}).All(&cites)
看到一个相关的/可能重复的问题:
$literal usage in Golang-mgo
关于mongodb - 如何使用mgo编写查询$ centerSphere,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43659678/