func connectToMongoDB() {
if client != nil {
return
}
var connectionError error
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
client, connectionError = mongo.Connect(ctx, options.Client().ApplyURI(mongoURI))
fmt.Printf("%+v", client)
collection = client.Database(dbName).Collection(col) // <--- since I'm only using one collection this can be global
defer cancel()
if connectionError != nil {
fmt.Printf("this is an eror %+v", connectionError)
}
}
view raw pt-3.main.go hosted with ❤ by GitHub