From 644d90bd1382f9211797ef39dee948298d53ec01 Mon Sep 17 00:00:00 2001 From: Zhenhang Tung Date: Fri, 2 Apr 2021 01:15:48 +0800 Subject: [PATCH] Update FindOne error handling in README (#623) --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1ec7d53c8..29ef77b91f 100644 --- a/README.md +++ b/README.md @@ -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...