[GODRIVER-1072] FindOneAndUpdate returns no error when no document found according to the filter criteria Created: 20/May/19  Updated: 27/Oct/23  Resolved: 20/May/19

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 1.0.1
Fix Version/s: None

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

Linux ubuntu 18.04 LTS



 Description   

I have this schema 

 

{
  "name": "PCP"
  "age": 20
}

I am using `FindOneAndUpdate()` to find the document and update it.

Its working fine for the cases in which there is a document present in the database as per the filter criteria. 

For cases in which there exist not document in the database as per the filter criteria, it returns no errors. What is expected that if there exist not document as per the filter criteria, the function should return an error so that the end user know that the document he is trying to update does not exist. 

I am using the following code 

 
 

func (u *user) update(name string, age int) error {     
   collection := database.Collection(userCollection)     
   selector := bson.M{"name": name}     
   update := bson.M{"$set": bson.M{"age": age}}     
   ctx, cancelFunc := context.WithTimeout(context.Background(),        20*time.Second)     
   defer cancelFunc()     
   result := collection.FindOneAndUpdate(ctx, selector, update)     
   return result.Err() 
}

 
 



 Comments   
Comment by Ian Whalen (Inactive) [ 20/May/19 ]

Hey prakashpandey thanks a lot for the report - this is actually working as per the mongodb driver specification. we do however recognize that the actual user experience is sub-par so we've filed GODRIVER-1075 to allow you to use a hasResults method instead.

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