Return mongo.ErrNoDocument when no match for update?

101 views
Skip to first unread message

Christopher Sterling

unread,
Sep 20, 2019, 3:31:32 PM9/20/19
to mongodb-go-driver
Is it possible to force mongo-go-driver to return an error when something is not found during an update/replace/delete operation?
In mgo.v2, it would return an ErrNotFound if there was nothing found.
Now though, we have to look at the result objects from each update/replace/delete operation and compute if it is mongo.ErrNoDocument ourselves using MatchedCount/UpsertedCount.
Is there some writeconcern option or client SafeMode setting or somesuch thing that we can enable to get this style of behavior back?

Bradley Weston

unread,
Sep 20, 2019, 4:12:00 PM9/20/19
to mongodb-...@googlegroups.com
You can do this test yourself on the result. It isn’t an error but an indicator.

--
You received this message because you are subscribed to the Google Groups "mongodb-go-driver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-go-dri...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-go-driver/9614469f-33d8-4724-9a4a-19f353c3406d%40googlegroups.com.

Christopher Sterling

unread,
Sep 20, 2019, 4:25:15 PM9/20/19
to mongodb-...@googlegroups.com
Which is exactly what we ended up doing - we created this beast:
// ResultIsNotFound takes a result from an Update/Insert/Delete
// operation and determines from the info if this is a true
// NotFound error.
func ResultIsNotFound(result interface{}) (err error) {
    if result == nil {
        // If there's no result, we don't know
        // what the error should be
        return nil
    }
    switch res := result.(type) {
    case *mongo.UpdateResult:
        if res.MatchedCount == 0 &&
            res.ModifiedCount == 0 &&
            res.UpsertedCount == 0 {
            err = NotFound
        }
    case mongo.UpdateResult:
        if res.MatchedCount == 0 &&
            res.ModifiedCount == 0 &&
            res.UpsertedCount == 0 {
            err = NotFound
        }
    case *mongo.BulkWriteResult:
        if res.MatchedCount == 0 &&
            res.ModifiedCount == 0 &&
            res.UpsertedCount == 0 {
            err = NotFound
        }
    case mongo.BulkWriteResult:
        if res.MatchedCount == 0 &&
            res.ModifiedCount == 0 &&
            res.UpsertedCount == 0 {
            err = NotFound
        }
    case *mongo.DeleteResult:
        if res.DeletedCount == 0 {
            err = NotFound
        }
    case mongo.DeleteResult:
        if res.DeletedCount == 0 {
            err = NotFound
        }
    case *mongo.InsertManyResult:
    case mongo.InsertManyResult:
    case *mongo.InsertOneResult:
    case mongo.InsertOneResult:
    case *mongo.SingleResult:
    case mongo.SingleResult:
        err = nil
    default:
        // Unrecognized case
        err = fmt.Errorf("a type of '%T' was not accounted for in db.IsErrNotFound", res)
        logrus.WithFields(logrus.Fields{
            "err": err,
        }).Errorf("Please check your usage of db.IsErrNotFound")
    }
    return err
}
I was just wondering if there was a strict mode where a not found could be enforced?

Divjot Arora

unread,
Oct 8, 2019, 2:25:22 PM10/8/19
to mongodb-go-driver
The MongoDB specifications don't treat a document not being found as an error, so it is currently not possible to do this. The only exception made in the Go driver is for the SingleResult type, which can return mongo.ErrNoDocuments from it's Err, Decode, and DecodeBytes functions if there were no document(s) found.

Christopher Sterling

unread,
Oct 8, 2019, 3:41:43 PM10/8/19
to mongodb-...@googlegroups.com
Hey Divjot -
Thanks a ton for answering :-)
Do you find it odd that different functions have different behaviors though?
I am willing to submit a PR against the codebase that allows for some kind of mode that will allow all functions to have the same behavior, but I don't want to write it if there's no chance of acceptance.

Divjot Arora

unread,
Oct 9, 2019, 11:30:14 AM10/9/19
to mongodb-go-driver
I do find it a little weird for functions to have different error behavior based on the return type, but I don't think we can change this in the near future as this would cause breaking change in our API.

Christopher Sterling

unread,
Oct 9, 2019, 11:46:47 AM10/9/19
to mongodb-...@googlegroups.com
I was thinking we could make it backwards compatible by making it a setting that has to be enabled. I was thinking the place it belonged was in the writeconcern preferences.
Something like RaiseErrorOnNotFound
Reply all
Reply to author
Forward
0 new messages