From c60e90701d3abaa19011ade81aeebd46bb96dc44 Mon Sep 17 00:00:00 2001 From: Shane Howearth Date: Tue, 26 Nov 2019 15:45:03 +1100 Subject: [PATCH 1/4] Add comment on exported Person struct --- mongodb/golang/examples.go | 1 + 1 file changed, 1 insertion(+) diff --git a/mongodb/golang/examples.go b/mongodb/golang/examples.go index f1770e8..677b743 100644 --- a/mongodb/golang/examples.go +++ b/mongodb/golang/examples.go @@ -10,6 +10,7 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) +// Person - type Person struct { Name string Age int From d3a5c847c59f048fe0c085ee4f112a7ece734fbb Mon Sep 17 00:00:00 2001 From: Shane Howearth Date: Tue, 26 Nov 2019 15:46:58 +1100 Subject: [PATCH 2/4] Use localhost instead of mongodb - using localhost means that a mongo instance either in a container or locally installed can be listening to localhost:27017 --- mongodb/golang/examples.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongodb/golang/examples.go b/mongodb/golang/examples.go index 677b743..4f34428 100644 --- a/mongodb/golang/examples.go +++ b/mongodb/golang/examples.go @@ -20,7 +20,7 @@ type Person struct { func main() { // Set client options - clientOptions := options.Client().ApplyURI("mongodb://mongodb:27017") + clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") // Connect to MongoDB client, err := mongo.Connect(context.TODO(), clientOptions) From bc176530a6db1f5ef7e8e9665194f40b191247b2 Mon Sep 17 00:00:00 2001 From: Shane Howearth Date: Tue, 26 Nov 2019 15:48:31 +1100 Subject: [PATCH 3/4] Use correct Person instance names and use struct field names --- mongodb/golang/examples.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mongodb/golang/examples.go b/mongodb/golang/examples.go index 4f34428..c730ff6 100644 --- a/mongodb/golang/examples.go +++ b/mongodb/golang/examples.go @@ -45,14 +45,14 @@ func main() { frankie := Person{"Frankie", 31, "Nairobi"} // Insert a single document - insertResult, err := collection.InsertOne(context.TODO(), ash) + insertResult, err := collection.InsertOne(context.TODO(), ruan) if err != nil { log.Fatal(err) } fmt.Println("Inserted a single document: ", insertResult.InsertedID) // Insert multiple documents - trainers := []interface{}{misty, brock} + trainers := []interface{}{james, frankie} insertManyResult, err := collection.InsertMany(context.TODO(), trainers) if err != nil { @@ -61,11 +61,11 @@ func main() { fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs) // Update a document - filter := bson.D{{"name", "Ash"}} + filter := bson.D{{Key: "name", Value: "Frankie"}} update := bson.D{ - {"$inc", bson.D{ - {"age", 1}, + {Key: "$inc", Value: bson.D{ + {Key: "age", Value: 1}, }}, } From 632e104449fea2c265e7cee8d9d635f99234de01 Mon Sep 17 00:00:00 2001 From: Shane Howearth Date: Tue, 26 Nov 2019 15:48:52 +1100 Subject: [PATCH 4/4] Unshadow err --- mongodb/golang/examples.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mongodb/golang/examples.go b/mongodb/golang/examples.go index c730ff6..6b051b1 100644 --- a/mongodb/golang/examples.go +++ b/mongodb/golang/examples.go @@ -99,15 +99,15 @@ func main() { // Iterate through the cursor for cur.Next(context.TODO()) { var elem Person - err := cur.Decode(&elem) - if err != nil { - log.Fatal(err) + inerr := cur.Decode(&elem) + if inerr != nil { + log.Fatal(inerr) } results = append(results, &elem) } - if err := cur.Err(); err != nil { + if err = cur.Err(); err != nil { log.Fatal(err) }