gpt4 book ai didi

mongodb - UpdateOne 因 E11000 重复键错误而失败

转载 作者:数据小太阳 更新时间:2023-10-29 03:19:03 27 4
gpt4 key购买 nike

我正在尝试从 go 更新 MongoDB 的文档。

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
c, _ := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
col := c.Database("epgrec").Collection("rec")
filter := bson.M{"_id": r.Mid}
update := bson.M{
"$set": bson.M{
"rid": r.Rid,
"channel": r.Channel,
"sid": r.Sid,
"pid": r.Pid,
"start": r.Start,
"duration": r.Duration,
"title": r.Title,
"status": r.Status,
"cmdarray": r.CmdArray,
"workingdir": r.WorkingDir,
},
}
res, err := col.UpdateOne(ctx, filter, update)

下面有错误。

multiple write errors: [{write errors: [{E11000 duplicate key error collection: epgrec.rec index: _id_ dup key: { : ObjectId('5d163936ead22a4f85f1e607') }}]}, {<nil>}]
exit status 1

使用 MongoDB Driver v1.0.3(来自 go.mongodb.org/mongo-driver/mongo)

如何避免这个错误?请帮忙。

最佳答案

好像有你的对象映射已经存在id的记录,我建议你阅读有关unique indexes的相关文档和 sparse indexes

关于mongodb - UpdateOne 因 E11000 重复键错误而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56810143/

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