Mongodb UpdateOne失败,出现E11000重复密钥错误
我正在尝试从go更新MongoDB的文档Mongodb UpdateOne失败,出现E11000重复密钥错误,mongodb,go,mongo-go,Mongodb,Go,Mongo Go,我正在尝试从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 := bs
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
使用go.MongoDB.org/mongo-Driver/mongo中的MongoDB驱动程序v1.0.3
如何避免此错误?请提供帮助。似乎有记录表明您的id对象映射已经存在,我鼓励您阅读有关和的相关文档您好,mkiuchi,欢迎来到stack overflow。请将您的问题更新为一个完整且可验证的示例。我应该能够运行您的代码并重现结果。在创建完整示例的过程中,请确保尽可能简单-这可能很难,但在这个过程中,您会经常找到答案。有关更多信息,请参见此处。是否可以运行db.rec.getIndexes并共享输出?可能相关: