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

FindOneAndUpdate with options.After is not returning the updated document

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.13.1
    • Component/s: None
    • None

      Detailed steps to reproduce the problem?

      Include full traceback, if possible

      I've taken over a project that has a method like the following:

      func (o *Object) UpdateRemovePrimaryImage() error {
          o.Updated = time.Now().UTC()
          o.PrimaryImage = nil

          fmt.Println("before db", o.PrimaryImage)
          if err := db.UpdateFields("objects", o.Id, o, o); err != nil {
              return err
      {{    }}}
          fmt.Println("after db", o.PrimaryImage)

          return nil
      }

      That method calls this function:

      func UpdateFields(col, id string, fields, res interface{}) error {
          collection := client.Database(name).Collection(col)

          filter := bson.D{{Key: "id", Value: id}}
          update := bson.D{{Key: "$set", Value: fields}}
          if err := collection.FindOneAndUpdate(
              context.TODO(),
              filter,
              update,
              options.FindOneAndUpdate().SetReturnDocument(options.After),
          ).Decode(res); err != nil {
              return err
      {{    }}}

          return nil
      }

      In the terminal, this is outputted:
      before db <nil>
      {{after db &{img_Yd2nsRFnjbjKyrLvHIBPHFmwB7j6J0E18KI34 7af964cb-95d4-48ec-89c0-69987779b33f robot.jpg }}}

      This isn't how I would do things, but I'm surprised that the original document is being returned. Is this expected behavior?

      Definition of done: what must be done to consider the task complete?

      The exact Go version used, with patch level:

      $ go version

      go version go1.21.5 linux/amd64

      The exact version of the Go driver used:

      $ go list -m go.mongodb.org/mongo-driver

      go.mongodb.org/mongo-driver v1.13.1

      Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.

      M10 hosted on Atlas, 7.0.4.

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      Linux 6.6.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 14 Dec 2023 03:45:42 +0000 x86_64 GNU/Linux

      Security Vulnerabilities

      If you’ve identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here

            Assignee:
            matt.dale@mongodb.com Matt Dale
            Reporter:
            dwrz@museumexchange.com David Wen Riccardi-Zhu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: