FindOneAndUpdate returns no error when no document found according to the filter criteria

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Major - P3
    • None
    • Affects Version/s: 1.0.1
    • Component/s: BSON
    • None
    • Environment:
      Linux ubuntu 18.04 LTS
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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() 
      }

       
       

            Assignee:
            Unassigned
            Reporter:
            prakash
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: