Support
Unable to connect using Go
over 4 years ago by Nathaniel Peiffer(edited)
Hey,
Just trying to connect to the mongodb instance using the standard mongo-driver package in go, however I am unable to establish a connection this way. I can log in perfectly fine using the latest version of the MongoDB shell on Windows.
My code for connecting is this:
package main
import (
"context"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func GetClient() *mongo.Client {
clientOptions := options.Client().ApplyURI("mongodb://testuser:********@test-server.provendb.io/test-server?ssl=true")
client, err := mongo.NewClient(clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Connect(context.Background())
if err != nil {
log.Fatal(err)
}
return client
}
func main() {
c := GetClient()
err := c.Ping(context.Background(), readpref.Primary())
if err != nil {
log.Fatal("Couldn't connect to the database - ", err)
} else {
log.Println("Connected!")
}
}
Any help would be awesome.