'How can i solve my Error by Connecting golang with MongoDB?
I have a problem with connecting the MongoDB to an API that is written in Golang. If you can help me, I would be really grateful. I don't find anything when I google it. To give a little context I am trying to complete this tutorial and in my opinion I have done it exactly like the guy from this medium post: https://medium.com/geekculture/build-a-rest-api-with-golang-and-mongodb-gin-gonic-version-6213083a86fe#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6ImIxYTgyNTllYjA3NjYwZWYyMzc4MWM4NWI3ODQ5YmZhMGExYzgwNmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJuYmYiOjE2NTI0Mjc1NjAsImF1ZCI6IjIxNjI5NjAzNTgzNC1rMWs2cWUwNjBzMnRwMmEyamFtNGxqZGNtczAwc3R0Zy5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsInN1YiI6IjExMDUzMzkxMjYxNjQ3MTI5NzcyOCIsImVtYWlsIjoiZWxpYWguZ2VyYmVyQGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJhenAiOiIyMTYyOTYwMzU4MzQtazFrNnFlMDYwczJ0cDJhMmphbTRsamRjbXMwMHN0dGcuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJuYW1lIjoiRWxpYWggR2VyYmVyIiwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hLS9BT2gxNEdpRXFEN1lzRFJBdTE2YWluUVkyQkVOQjhILUc5XzFCNEVvdElJQjB3PXM5Ni1jIiwiZ2l2ZW5fbmFtZSI6IkVsaWFoIiwiZmFtaWx5X25hbWUiOiJHZXJiZXIiLCJpYXQiOjE2NTI0Mjc4NjAsImV4cCI6MTY1MjQzMTQ2MCwianRpIjoiMzYxMzAwZjQyMDE4M2Q4MjU5MzY5ODQwNmNlYjY0YmE0MWE4NWJhZCJ9.llhfgrPC1LaGzaf6evc-AhUaMsSJl529gC_Uc5BA1o1X9zzz0xxPtACth719hmYukLgnpKeU3UFTi74fUfHBA3vmg-9QgJY2vUamiiZNOJ5ZcufJEUAd37mqsOBA2_xghsr34aTKypO741pBsx-LDOa7fcIwTyeTrgJL3pQAYYPyri7O27eEsW-Khb3SWhnYpjv-WB7Eph6c4_jyNeiWf6CFAhMl4Xl-gbMaTiT2YIF9W_oNfQQyZ7OPMi0w1l2_7wSIxbTRrrGs_mlXZgaM5lHVqTh32G9SVNpsV6GGC57a1zISioKLEgEgGrphQ-7BBJ89Jqv_uewM8jVOMaFt7Q
My error:
2022/05/13 12:24:55 server selection error: context deadline exceeded, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: cluster0-shard-00-01.muyyt.mongodb.net:27017, Type: Unknown }, { Addr: cluster0-shard-00-02.m
uyyt.mongodb.net:27017, Type: Unknown }, { Addr: cluster0-shard-00-00.muyyt.mongodb.net:27017, Type: Unknown }, ] }
My Files: .env
MONGOURI=mongodb+srv://eliah:[email protected]/myFirstDatabase?retryWrites=true&w=majority
env.go
package configs
import (
"github.com/joho/godotenv"
"log"
"os"
)
func EnvMongoURI() string {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
return os.Getenv("MONGOURI")
}
setup.go:
package configs
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"
)
func ConnectDB() *mongo.Client {
client, err := mongo.NewClient(options.Client().ApplyURI(EnvMongoURI()))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
//ping the database
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB")
return client
}
//Client instance
var DB *mongo.Client = ConnectDB()
//getting database collections
func GetCollection(client *mongo.Client, collectionName string) *mongo.Collection {
collection := client.Database("golangAPI").Collection(collectionName)
return collection
}
main.go:
package main
import (
"gin-mongo-api/configs" //add this
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
//run database
configs.ConnectDB()
router.Run("localhost:6000")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|