src/pkg/client/mongodb/client.go
package mongodb
import (
"context"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// ConnectMongoDb take mongodb url and related to connections
func ConnectMongoDb(url string) (*mongo.Client, error) {
clientOptions := options.Client().ApplyURI(url)
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
return nil, err
}
// Check the connection
if err = client.Ping(context.TODO(), nil); err != nil {
return nil, err
}
logrus.Info("MongoClient connected")
return client, nil
}