[英]Upsert not working when using UpdateOne with the MongoDB Golang driver
For reference, I have this struct:作为参考,我有这个结构:
type OpenOrderCleaned struct {
OrderID string `json:"orderId" bson:"orderId"`
DateTimeOrderPlaced time.Time `json:"dateTimeOrderPlaced" bson:"dateTimeOrderPlaced"`
OrderItems []struct {
OrderItemID string `json:"orderItemId" bson:"orderItemId"`
Ean string `json:"ean" bson:"ean"`
CancelRequest bool `json:"cancelRequest" bson:"cancelRequest"`
Quantity int `json:"quantity" bson:"quantity"`
} `json:"orderItems" bson:"orderItems"`
}
I get an API response with multiple JSON instances that I want to save in MongoDB, so I use a for loop.我收到了一个包含多个 JSON 实例的 API 响应,我想将这些实例保存在 MongoDB 中,因此我使用了 for 循环。 I want to check if a document already exists in the database, by using the
orderId
field, which is unique for every JSON instance.我想通过使用
orderId
字段检查数据库中是否已存在文档,该字段对于每个 JSON 实例都是唯一的。 I thought UpdateOne
was a good option for this because it has upsert
.我认为
UpdateOne
是一个不错的选择,因为它具有upsert
。 So if the orderId does not exist, the full document should be generated and stored in the database.因此,如果 orderId 不存在,则应生成完整的文档并将其存储在数据库中。
for _, OpenOrderCleaned := range o.Orders {
c := auth.GetClient()
collection := c.Database("goprac").Collection(x)
filter := bson.M{"orderId": bson.M{"$eq": OpenOrderCleaned.OrderID}}
update := bson.M{
"$set": bson.M{
"orderId": OpenOrderCleaned.OrderID,
"dateTimeOrderPlaced": OpenOrderCleaned.DateTimeOrderPlaced,
"orderItems": OpenOrderCleaned.OrderItems,
},
}
ctx, _ := context.WithTimeout(context.Background(), 15*time.Second)
result, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
fmt.Println("UpdateOne() result ERROR:", err)
os.Exit(1)
} else {
fmt.Println("UpdateOne() result:", result)
fmt.Println("UpdateOne() result TYPE:", reflect.TypeOf(result))
fmt.Println("UpdateOne() result MatchedCount:", result.MatchedCount)
fmt.Println("UpdateOne() result ModifiedCount:", result.ModifiedCount)
fmt.Println("UpdateOne() result UpsertedCount:", result.UpsertedCount)
fmt.Println("UpdateOne() result UpsertedID:", result.UpsertedID)
}
}
But right now the Upsert
is not working.但是现在
Upsert
不起作用。 When I put a manual document into MongoDB and running the program, it is updating though.当我将手动文档放入 MongoDB 并运行该程序时,它正在更新。 So why are new instances not made in the database?
那么为什么没有在数据库中创建新实例呢? Do I have to state somewhere
upsert=True
or something?我是否必须在某处声明
upsert=True
或其他什么? Or is maybe the mapping of "orderItems": OpenOrderCleaned.OrderItems
not correct?或者
"orderItems": OpenOrderCleaned.OrderItems
的映射"orderItems": OpenOrderCleaned.OrderItems
不正确?
Any help is appreciated.任何帮助表示赞赏。
You are calling update, not upsert.您正在调用更新,而不是 upsert。 You have to pass the correct options to
UpdateOne
to upsert.您必须将正确的选项传递给
UpdateOne
以进行 upsert。 This is from the mongo driver examples:这是来自 mongo 驱动程序示例:
opts := options.Update().SetUpsert(true)
filter := bson.D{{"_id", id}}
update := bson.D{{"$set", bson.D{{"email", "newemail@example.com"}}}}
result, err := coll.UpdateOne(context.TODO(), filter, update, opts)
You are missing the opts
.你错过了
opts
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.