我正在尝试从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驱动程序v1.0.3(来自go.mongodb.org/mongo-driver/mongo)
如何避免此错误?请帮忙。