[GODRIVER-770] Unexpected behaviour of FindOneAndUpdate method Created: 17/Jan/19  Updated: 27/Oct/23  Resolved: 29/Jan/19

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: 0.0.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Sumit Tembe Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I am using FindOneAndUpdate method to update the document. The document is getting updated in DB but when you do DecodeBytes() on SingleResult it giver err:

mongo: no documents in result

This is happening sometimes.

result := coll.FindOneAndUpdate(context.TODO(), filter, update)    
if _, err := result.DecodeBytes(); err != nil {        
     logger.Error(err)        
     return err    
}



 Comments   
Comment by Wan Bachtiar [ 29/Jan/19 ]

 Hi sumit.tembe,

First of all, I believe that you're meaning to specify current v0.2.0 instead of v0.0.2. The below answer would be based on current beta v0.2.0.

I could only replicate error that you have reported above, only if there is no matching documents to update. For example, the below snippet should work:

var filter = bson.D{{"item", "exist"}}
var update = bson.D{{"$set", bson.D{{"updated", "true"}}}}
result := collection.FindOneAndUpdate(context.TODO(), filter, update)
r, err := result.DecodeBytes()

While the below snippet, would return err during DecodeBytes():

var filter = bson.D{{"item", "DoesNotExist"}}
var update = bson.D{{"$set", bson.D{{"updated", "true"}}}}
result := collection.FindOneAndUpdate(context.TODO(), filter, update)
r, err := result.DecodeBytes()

This is because if there is no document matching the filter to be updated, SingleResult returns null. This is in line with the behaviour of findOneAndUpdate in mongo shell as well.

See also  SingleResult.DecodeBytes for more information. 

Regards,
Wan.

 

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