[GODRIVER-3080] FindOneAndUpdate with options.After is not returning the updated document Created: 28/Dec/23  Updated: 18/Jan/24

Status: Scheduled
Project: Go Driver
Component/s: None
Affects Version/s: 1.13.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: David Wen Riccardi-Zhu Assignee: Matt Dale
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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



 Comments   
Comment by PM Bot [ 28/Dec/23 ]

Hi dwrz@museumexchange.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Thu Feb 08 08:40:01 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.