Skip to content

Commit

Permalink
Update FindOne error handling in README (mongodb#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenhangTung authored Apr 1, 2021
1 parent 1c4c588 commit 644d90b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ filter := bson.D{{"name", "pi"}}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = collection.FindOne(ctx, filter).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
// Do something when no record was found
fmt.Println("record does not exist")
} else {
log.Fatal(err)
}
// Do something with result...
Expand Down

0 comments on commit 644d90b

Please sign in to comment.