我正在尝试使用库UpdateOne
,mongo-go-driver
但这种方法需要 bson 文档。我给它一个接口参数(json)。
我的问题是找到将我的 json 请求解析为 bson 以动态更新字段的最佳方法。谢谢你。
func (s Store) Update(id`enter code here` bson.D, d interface{}) (*mongo.UpdateResult, int32, string) {
upd := bson.D{
{
"$inc", bson.D{
d,
},
},
}
c, ctx, _ := getCollection(s.conn, s.dbName, s.collectionName)
res, err := c.UpdateOne(ctx, id, d)
if err != nil {
log.Fatal(err)
return res, 500, "DATABASE ERROR: Cannot update document"
}
return res, 200, "none"
}
我收到此错误:
Error: cannot use d (type inte`enter code here`rface {}) as type primitive.E in array or slice literal: need type assertion