Skip to content

Commit 72271e3

Browse files
author
Chris Cho
committed
Merge pull request #349 from ccho-mongodb/DOCS-16685-retrieve-data-example-fix
DOCS-16685: fix retrieve data fundamentals code example (cherry picked from commit 939a9e3)
1 parent 5a42ccc commit 72271e3

File tree

1 file changed

+20
-13
lines changed
  • source/includes/fundamentals/code-snippets/CRUD

1 file changed

+20
-13
lines changed

source/includes/fundamentals/code-snippets/CRUD/retrieve.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33

44
import (
55
"context"
6-
"encoding/json"
76
"fmt"
87
"log"
98
"os"
@@ -26,7 +25,7 @@ type Tea struct {
2625

2726
func main() {
2827
var uri string
29-
if uri = os.Getenv("DRIVER_REF_URI"); uri == "" {
28+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
3029
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable")
3130
}
3231

@@ -44,12 +43,12 @@ func main() {
4443
// begin insert docs
4544
coll := client.Database("db").Collection("tea")
4645
docs := []interface{}{
47-
Review{Item: "Masala", Rating: 10, DateOrdered: time.Date(2009, 11, 17, 0, 0, 0, 0, time.Local)},
48-
Review{Item: "Sencha", Rating: 7, DateOrdered: time.Date(2009, 11, 18, 0, 0, 0, 0, time.Local)},
49-
Review{Item: "Masala", Rating: 9, DateOrdered: time.Date(2009, 11, 12, 0, 0, 0, 0, time.Local)},
50-
Review{Item: "Masala", Rating: 8, DateOrdered: time.Date(2009, 12, 1, 0, 0, 0, 0, time.Local)},
51-
Review{Item: "Sencha", Rating: 10, DateOrdered: time.Date(2009, 12, 17, 0, 0, 0, 0, time.Local)},
52-
Review{Item: "Hibiscus", Rating: 4, DateOrdered: time.Date(2009, 12, 18, 0, 0, 0, 0, time.Local)},
46+
Tea{Item: "Masala", Rating: 10, DateOrdered: time.Date(2009, 11, 17, 0, 0, 0, 0, time.Local)},
47+
Tea{Item: "Sencha", Rating: 7, DateOrdered: time.Date(2009, 11, 18, 0, 0, 0, 0, time.Local)},
48+
Tea{Item: "Masala", Rating: 9, DateOrdered: time.Date(2009, 11, 12, 0, 0, 0, 0, time.Local)},
49+
Tea{Item: "Masala", Rating: 8, DateOrdered: time.Date(2009, 12, 1, 0, 0, 0, 0, time.Local)},
50+
Tea{Item: "Sencha", Rating: 10, DateOrdered: time.Date(2009, 12, 17, 0, 0, 0, 0, time.Local)},
51+
Tea{Item: "Hibiscus", Rating: 4, DateOrdered: time.Date(2009, 12, 18, 0, 0, 0, 0, time.Local)},
5352
}
5453

5554
result, err := coll.InsertMany(context.TODO(), docs)
@@ -80,7 +79,7 @@ func main() {
8079
panic(err)
8180
}
8281

83-
var results []Review
82+
var results []Tea
8483
if err = cursor.All(context.TODO(), &results); err != nil {
8584
panic(err)
8685
}
@@ -101,10 +100,14 @@ func main() {
101100

102101
// Retrieves a document that matches the filter and prints it as
103102
// a struct
104-
var result Review
103+
var result Tea
105104
err := coll.FindOne(context.TODO(), filter, opts).Decode(&result)
106105
if err != nil {
107-
panic(err)
106+
if err == mongo.ErrNoDocuments {
107+
fmt.Println("No documents found")
108+
} else {
109+
panic(err)
110+
}
108111
}
109112

110113
res, _ := bson.MarshalExtJSON(result, false, false)
@@ -127,10 +130,14 @@ func main() {
127130

128131
// Retrieves a document that matches the filter and prints it as
129132
// a struct
130-
var result Review
133+
var result Tea
131134
err = coll.FindOne(context.TODO(), filter, opts).Decode(&result)
132135
if err != nil {
133-
panic(err)
136+
if err == mongo.ErrNoDocuments {
137+
fmt.Println("No documents found")
138+
} else {
139+
panic(err)
140+
}
134141
}
135142

136143
res, _ := bson.MarshalExtJSON(result, false, false)

0 commit comments

Comments
 (0)