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

I'm trying to update document of MongoDB from go.

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)

I've got error below.

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

using MongoDB Driver v1.0.3 (from go.mongodb.org/mongo-driver/mongo)

How do I avoid this error? Please help.

It seems as if there are records where your object mapping already exists for the id, I would encourage you to read the relevant documentation about unique indexes and sparse indexes