Last active
February 8, 2023 01:33
-
-
Save yashasolutions/2ddaccb2bb26b9b83d696987517fa6ac to your computer and use it in GitHub Desktop.
node-db-test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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