Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2879

BulkWrite: Performing an update on the path '_id'

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 1.12.0
    • Component/s: Bulk API
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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

            Assignee:
            qingyang.hu@mongodb.com Qingyang Hu
            Reporter:
            mymmrac@gmail.com Artem Yadelskyi
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: