登录
首页 >  Golang >  Go问答

UpdateOne 失败,出现 E11000 重复密钥错误

来源:stackoverflow

时间:2024-04-18 21:33:32 235浏览 收藏

golang学习网今天将给大家带来《UpdateOne 失败,出现 E11000 重复密钥错误》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

问题内容

我正在尝试从 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') }}]}, {}]
exit status 1

使用 mongodb 驱动程序 v1.0.3(来自 go.mongodb.org/mongo-driver/mongo)

如何避免此错误?请帮忙。


解决方案


似乎有记录表明您的对象映射已存在该 id,我建议您阅读有关 unique indexessparse indexes 的相关文档

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>