Skip to content

Instantly share code, notes, and snippets.

@yashasolutions
Last active February 8, 2023 01:33
Show Gist options
  • Save yashasolutions/2ddaccb2bb26b9b83d696987517fa6ac to your computer and use it in GitHub Desktop.
Save yashasolutions/2ddaccb2bb26b9b83d696987517fa6ac to your computer and use it in GitHub Desktop.
node-db-test
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// Connection URI
//const uri = "mongodb:///?maxPoolSize=20&w=majority"
const uri = `mongodb://${process.env.MONGO_HOST}:${process.env.MONGO_PORT}/${process.env.MONGO_NAME}`;
func main() {
// Create a new client and connect to the server
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
// Ping the primary
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
panic(err)
}
fmt.Println("Successfully connected and pinged.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment