Skip to content

Commit ea10fd8

Browse files
author
Amit
committed
updated go version and added multi database support
1 parent d18ed61 commit ea10fd8

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Set up Go
2020
uses: actions/setup-go@v4
2121
with:
22-
go-version: '1.23.6'
22+
go-version: '1.24.5'
2323

2424
- name: Build
2525
run: go build -v ./...

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/amitjangid80/go-mongodb-client
22

3-
go 1.23.6
3+
go 1.24.5
44

55
require go.mongodb.org/mongo-driver v1.17.4
66

mongodb_client/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package mongodb_client
22

3-
type MongodbConfig struct {
3+
type MongodbConfigV2 struct {
44
Username string
55
Password string
66
Host string
77
Port string
8-
DbName string
98
}

mongodb_client/mongodb-client.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,50 @@ import (
1111
"go.mongodb.org/mongo-driver/mongo/options"
1212
)
1313

14-
var mongodb *mongo.Database
15-
16-
// GetDb returns the mongodb client.
17-
func GetDb() *mongo.Database {
18-
return mongodb
19-
}
14+
var client *mongo.Client
2015

2116
// GetClient returns the mongodb client.
2217
func GetClient() *mongo.Client {
23-
return mongodb.Client()
18+
return client
2419
}
2520

26-
// ConnectDB initializes a MongoDB client and returns the database reference.
27-
func ConnectDb(config *MongodbConfig) {
21+
// GetDb returns the mongodb client.
22+
func GetDb(dbName string) *mongo.Database {
23+
return client.Database(dbName)
24+
}
25+
26+
// Connect Mongodb function will initialize and connect to mongodb based on the URL, Port and Host passed via config
27+
func ConnectDb(config *MongodbConfigV2) {
2828
// Get MongoDB URI from environment variable if set, otherwise use default
2929
mongoDbUrl := fmt.Sprintf("mongodb://%s:%s@%s:%s", config.Username, config.Password, config.Host, config.Port)
3030

3131
clientOpts := options.Client().ApplyURI(mongoDbUrl)
3232
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
3333
defer cancel()
3434

35-
client, err := mongo.Connect(ctx, clientOpts)
35+
var err error
36+
client, err = mongo.Connect(ctx, clientOpts)
3637

3738
if err != nil {
38-
log.Fatalf("Failed to connect to MongoDB: %v", err)
39+
log.Fatalf("Failed to connect to MongoDB: %v", err)
3940
}
4041

4142
// Verify connection
4243
if err := client.Ping(ctx, nil); err != nil {
43-
log.Fatalf("Failed to ping MongoDB: %v", err)
44+
log.Fatalf("Failed to ping MongoDB: %v", err)
4445
}
4546

46-
log.Printf("Connected to MongoDB on port: %s", config.Port)
47-
48-
mongodb = client.Database(config.DbName)
47+
log.Printf("✅ Connected to MongoDB on port: %s", config.Port)
4948
}
5049

5150
// Create Collections
5251
func CreateCollections(ctx context.Context, dbName string, collectionName string) {
5352
// List existing collections
54-
existingCollections, err := GetDb().ListCollectionNames(ctx, bson.D{})
53+
existingCollections, err := GetDb(dbName).ListCollectionNames(ctx, bson.D{})
5554
log.Printf("List of Collections in %s: %v", dbName, existingCollections)
5655

5756
if err != nil {
58-
log.Fatalf("Failed to list collections in DB %s: %v", dbName, err)
57+
log.Fatalf("Failed to list collections in DB %s: %v", dbName, err)
5958
}
6059

6160
existingMap := make(map[string]bool)
@@ -66,12 +65,12 @@ func CreateCollections(ctx context.Context, dbName string, collectionName string
6665

6766
// Create only missing collections
6867
if !existingMap[collectionName] {
69-
if err := GetDb().CreateCollection(ctx, collectionName); err != nil {
70-
log.Fatalf("Failed to create collection %s: %v", collectionName, err)
68+
if err := GetDb(dbName).CreateCollection(ctx, collectionName); err != nil {
69+
log.Fatalf("Failed to create collection %s: %v", collectionName, err)
7170
}
7271

73-
log.Printf("Created collection: %s", collectionName)
72+
log.Printf("Created collection: %s", collectionName)
7473
} else {
75-
log.Printf("Collection already present: %s", collectionName)
74+
log.Printf("Collection already present: %s", collectionName)
7675
}
7776
}

0 commit comments

Comments
 (0)