如何使用 mgo 编写查询 $centerSphere

How to write query $centerSphere using mgo

我已阅读文档 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{}类型的切片中传递中心点和半径,其中点也是包含其坐标的切片,也可以是[=类型12=].

err = collection.Find(bson.M{
    "location": bson.M{
        "$geoWithin": bson.M{
            "$centerSphere": []interface{}{
                []interface{}{-73.93414657, 40.82302903}, 5 / 3963.2,
            },
        },
    },
}).All(&cites)

查看相关/可能重复的问题: