From 2761a05c01972f143e9f39b0154a010c7479451b Mon Sep 17 00:00:00 2001 From: Nam Huynh Date: Fri, 8 Oct 2021 15:05:13 +0700 Subject: [PATCH] return mongo instance on connect (for multiple database connect) --- mongodb.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mongodb.go b/mongodb.go index d7e0d19..5741b17 100644 --- a/mongodb.go +++ b/mongodb.go @@ -12,7 +12,7 @@ import ( var db *mongo.Database // Connect to mongo server -func Connect(host, user, password, dbName, mechanism, source string) error { +func Connect(host, user, password, dbName, mechanism, source string) (*mongo.Database, error) { connectOptions := options.ClientOptions{} // Set auth if existed if user != "" && password != "" { @@ -28,14 +28,14 @@ func Connect(host, user, password, dbName, mechanism, source string) error { client, err := mongo.Connect(context.Background(), connectOptions.ApplyURI(host)) if err != nil { fmt.Println("Error when connect to MongoDB database", host, err) - return err + return nil, err } fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + host)) // Set data db = client.Database(dbName) - return nil + return db, nil } // GetInstance ...