gpt4 book ai didi

mongodb - 将文档数字字段更新为值 0 不起作用

转载 作者:行者123 更新时间:2023-12-01 22:21:44 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to update Mongodb fields with omitempty flag in Golang structure

(2 个回答)


1年前关闭。




我有一个 Go 函数,它使用新值更新 MongoDB 文档,它适用于除 0 以外的任何值。我不明白为什么。它返回 0 的 ModifiedCount。
例如,我可以将“价格”字段从 75 更新为 20。但它不会将其从 75 更新为 0。重要部分如下所示:

type Car struct {
ID primitive.ObjectID `bson:"_id,omitempty" `
Price float64 `json:"price" form:"price" bson:"price,omitempty"`
}
...
updateResult, err := c.UpdateOne(ctx, filter, bson.M{"$set": update}, options)
在哪里:
筛选:
 {ObjectID("5f1aa6da68ac05d7863e9b41")}
并更新:
 {ObjectID("5f1aa6da68ac05d7863e9b41") 0}

最佳答案

我不太确定,但我认为这是由于 omitempty统治你的 bson,因为 nil不能是浮点数的有效值,0值省略您的字段。更多解释here .如果该字段可以设置为空值,我建议您使用 this approach .

关于mongodb - 将文档数字字段更新为值 0 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63073115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com