我需要使用Go Gogo官方驱动程序进行Camera.Parameters
操作。
在MongoDB中,我们有一些文档:
video-stabilization-supported=false
然后执行addToSet
:
{ _id: 2, item: "cable", tags: [ "electronics", "supplies" ] }
结果:
addToSet
答案 0 :(得分:1)
$addToSet
是一项更新操作,如果要更新单个文档,则可以使用Collection.UpdateOne()
方法。
使用bson.M
和/或bson.D
类型描述您的过滤器并更新文档。
例如:
update := bson.M{
"$addToSet": bson.M{
"tags": bson.M{"$each": []string{"camera", "electronics", "accessories"}},
},
}
res, err := c.UpdateOne(ctx, bson.M{"_id": 2}, update)
这是一个完整的,可运行的应用程序,可连接到MongoDB服务器并执行上述更新操作:
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost"))
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
c := client.Database("dbname").Collection("inventory")
update := bson.M{
"$addToSet": bson.M{
"tags": bson.M{"$each": []string{"camera", "electronics", "accessories"}},
},
}
res, err := c.UpdateOne(ctx, bson.M{"_id": 2}, update)
if err != nil {
panic(err)
}
fmt.Printf("%+v", res)