Skip to content

Instantly share code, notes, and snippets.

@JNaeemGitonga
Last active December 30, 2019 21:45
Show Gist options
  • Save JNaeemGitonga/0b071d25c99c016e0cd3a805e527539c to your computer and use it in GitHub Desktop.
Save JNaeemGitonga/0b071d25c99c016e0cd3a805e527539c to your computer and use it in GitHub Desktop.
func connectToMongoDB() {...}
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment