我正在尝试使用结构更新/替换mongodb文档,但我一直在获取err: update document must contain key beginning with '$'
collection := r.client.Database(database).Collection(greetingCollection)
payment.MongoID = objectid.New()
filter := bson.NewDocument(bson.EC.String("id", payment.ID))
_, err := collection.UpdateOne(ctx, filter, payment)
return err发布于 2018-10-07 20:08:48
应该将update语句而不是文档作为Collection.UpdateOne方法的第三个参数。例如:
update := bson.NewDocument(
bson.EC.SubDocumentFromElements(
"$set",
bson.EC.Double("pi", 3.14159),
),
)
collection.UpdateOne(ctx, filter, update)有关MongoDB docs中可用的更新操作符(键以‘$’开头)的更多信息,请参阅。
发布于 2020-03-31 16:09:45
我相信被接受的答案对我不起作用,因为我使用的是go.mongodb.org/mongo-driver包。使用这个包,语法甚至更简单:
update := bson.M{
"$set": yourDocument,
}
collection.UpdateOne(ctx, filter, update)https://stackoverflow.com/questions/52684304
复制相似问题