Details
-
Bug
-
Resolution: Works as Designed
-
Minor - P4
-
None
-
1.12.0
-
None
Description
In go.mod:
go.mongodb.org/mongo-driver v1.12.0
|
Example code:
type Model struct {
|
ID uint64 `bson:"_id"`
|
Data string `bson:"data"`
|
}
|
|
|
func UpdateMany(ctx context.Context, coll *mongo.Collection, models []Model) error {
|
update := make([]mongo.WriteModel, 0, len(models))
|
for _, model := range models {
|
update = append(update,
|
mongo.NewUpdateOneModel().
|
SetFilter(bson.D{{"_id", model.ID}}).
|
SetUpdate(bson.D{{"$set", model}}),
|
)
|
}
|
_, err := coll.BulkWrite(ctx, update)
|
return err
|
}
|
Error when calling UpdateMany:
bulk write exception: write errors: [Performing an update on the path '_id' would modify the immutable field '_id']
|
If running the same query from mongosh there will be no error, also if ID field will have complex type (for example struct with 2 fields) - also no error