GoLang mgo - mgo.ErrNotFound for searching (...). All (...)

I have GoLang code:

c.Find(selectQuery).All(&results)
if err == mgo.ErrNotFound {
// error handling
}

      

selectQuery

The meaning is not important here.

I never get the error ErrNotFound

. Even if the request doesn't match any results, I don't get it ErrNotFound

. I am getting a variable result

with empty attributes. How do I change the code to handle the ErrNotFound

case?

+3


source to share


1 answer


Query.All()

never returns mgo.ErrNotFound

, so it's useless to check that. If there are no results, the length results

will be 0, so you might find that if there were no errors:

err := c.Find(selectQuery).All(&results)
if err != nil { {
    // error handling
    return
}
// If you must detect "not found" case:
if len(results) == 0 {
    // No results
}

      



mgo.ErrNotFound

used / returned by other methods, usually those that should work on the same document, like Query.One()

or Query.Apply()

.

+5


source







All Articles