File tree Expand file tree Collapse file tree 2 files changed +6
-11
lines changed
fundamentals/code-snippets Expand file tree Collapse file tree 2 files changed +6
-11
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,6 @@ package main
33import (
44 "context"
55 "fmt"
6- "time"
76
87 "go.mongodb.org/mongo-driver/mongo"
98 "go.mongodb.org/mongo-driver/mongo/options"
@@ -14,23 +13,20 @@ import (
1413const uri = "mongodb://user:[email protected] :27017/?maxPoolSize=20&w=majority" 1514
1615func main () {
17- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
18- defer cancel ()
19-
2016 // Create a new client and connect to the server
21- client , err := mongo .Connect (ctx , options .Client ().ApplyURI (uri ))
17+ client , err := mongo .Connect (context . TODO () , options .Client ().ApplyURI (uri ))
2218
2319 if err != nil {
2420 panic (err )
2521 }
2622 defer func () {
27- if err = client .Disconnect (ctx ); err != nil {
23+ if err = client .Disconnect (context . TODO () ); err != nil {
2824 panic (err )
2925 }
3026 }()
3127
3228 // Ping the primary
33- if err := client .Ping (ctx , readpref .Primary ()); err != nil {
29+ if err := client .Ping (context . TODO () , readpref .Primary ()); err != nil {
3430 panic (err )
3531 }
3632
Original file line number Diff line number Diff line change @@ -13,20 +13,19 @@ import (
1313)
1414
1515func main () {
16- ctx := context .TODO ()
1716 uri := os .Getenv ("MONGODB_URI" )
1817 if uri == "" {
1918 log .Panic (`'uri' is empty. Ensure you set the 'MONGODB_URI'
2019 environment variable, or set the value of 'uri' to your
2120 Atlas connection string.` )
2221 }
23- client , err := mongo .Connect (ctx , options .Client ().ApplyURI (uri ))
22+ client , err := mongo .Connect (context . TODO () , options .Client ().ApplyURI (uri ))
2423 if err != nil {
2524 log .Panic (err )
2625 }
2726
2827 defer func () {
29- if err := client .Disconnect (ctx ); err != nil {
28+ if err := client .Disconnect (context . TODO () ); err != nil {
3029 log .Panic (err )
3130 }
3231 }()
@@ -35,7 +34,7 @@ func main() {
3534 title := "Back to the Future"
3635
3736 var result bson.M
38- err = coll .FindOne (ctx , bson.D {{"title" , title }}).Decode (& result )
37+ err = coll .FindOne (context . TODO () , bson.D {{"title" , title }}).Decode (& result )
3938 if err == mongo .ErrNoDocuments {
4039 fmt .Printf ("No document was found with the title %s\n " , title )
4140 return
You can’t perform that action at this time.
0 commit comments