|
kush.patel@mongodb.com thank you for reporting!
It seems this behavior is expected when decoding a *SingleResult with no return document, see here. However, there are at least two idiomatic ways your team can approach handling this.
1. Skip the error
The mongo: no documents in result error is declared and exported by the mongo package and can be used to programmatically "skip" propagation:
if err := sr.Decode(v); err != nil && err != mongo.ErrNoDocuments {
|
log.Fatal(err)
|
}
|
2. Set the return document
The options package defines a SetReturnDocument that can be used to return documents in a state before or after the operation:
coll := t.Client.Database("test").Collection("coll")
|
opts := options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)
|
filter := bson.D{{"doesnotexist", "hi"}}
|
update := bson.D{{"$set", bson.D{{"hello", "world"}}}}
|
result := coll.FindOneAndUpdate(context.TODO(), filter, update, opts)
|
var decodedResult interface{}
|
if err := result.Decode(&decodedResult); err != nil {
|
log.Fatal(err)
|
}
|
fmt.Println(decodedResult) // [{_id ObjectID("62588daf4d24ebb443c02121")} {doesnotexist hi} {hello world}]
|
|