Skip to content

Commit 7662ecc

Browse files
author
Amit
committed
Removed Fatal Logs and Updated Create Collection Function in Readme.
1 parent ed7687b commit 7662ecc

File tree

2 files changed

+12
-45
lines changed

2 files changed

+12
-45
lines changed

Readme.md

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,8 @@ func GetDb() {
4949

5050
```go
5151
// Create Collections
52-
func CreateCollections(config *config.Config) {
53-
bgCtx := context.Background()
54-
ctx, cancel := context.WithTimeout(bgCtx, 5*time.Second)
55-
defer cancel()
56-
57-
collections := []string{
58-
// list of your collections which you want to create under a database
59-
}
60-
61-
// List existing collections
62-
existingCollections, err := mongodb_client.GetDb().ListCollectionNames(ctx, bson.D{})
63-
log.Printf("✅ List of Collections: %s", existingCollections)
64-
65-
if err != nil {
66-
log.Fatalf("❌ Failed to list collections in DB %s: %v", config.ClientDbName, err)
67-
}
68-
69-
existingMap := make(map[string]bool)
70-
71-
for _, name := range existingCollections {
72-
existingMap[name] = true
73-
}
74-
75-
log.Printf("✅ Collections already present: %v", existingMap)
76-
77-
// Create only missing collections
78-
for _, collection := range collections {
79-
if !existingMap[collection] {
80-
if err := mongodb_client.GetDb("your_database_name").CreateCollection(ctx, collection); err != nil {
81-
log.Fatalf("❌ Failed to create collection %s: %v", collection, err)
82-
}
83-
84-
log.Printf("✅ Created collection: %s", collection)
85-
}
86-
}
52+
func CreateCollections() {
53+
mongodb_client.CreateCollection("YOUR_DB_NAME", "YOUR_COLLECTION_NAME")
8754
}
8855
```
8956

mongodb_client/mongodb-client.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ func ConnectDb(config *MongodbConfig) {
3838
client, err = mongo.Connect(ctx, clientOpts)
3939

4040
if err != nil {
41-
log.Fatalf("❌ Failed to connect to MongoDB: %v", err)
41+
log.Printf("❌ Failed to connect to MongoDB: %v", err)
4242
}
4343

4444
// Verify connection
4545
if err := client.Ping(ctx, nil); err != nil {
46-
log.Fatalf("❌ Failed to ping MongoDB: %v", err)
46+
log.Printf("❌ Failed to ping MongoDB: %v", err)
4747
}
4848

4949
log.Printf("✅ Connected to MongoDB on port: %s", config.Port)
@@ -59,7 +59,7 @@ func CreateCollection(dbName string, collectionName string) {
5959
log.Printf("List of Collections in %s: %v", dbName, existingCollections)
6060

6161
if err != nil {
62-
log.Fatalf("❌ Failed to list collections in DB %s: %v", dbName, err)
62+
log.Printf("❌ Failed to list collections in DB %s: %v", dbName, err)
6363
}
6464

6565
existingMap := make(map[string]bool)
@@ -71,7 +71,7 @@ func CreateCollection(dbName string, collectionName string) {
7171
// Create only missing collections
7272
if !existingMap[collectionName] {
7373
if err := GetDb(dbName).CreateCollection(ctx, collectionName); err != nil {
74-
log.Fatalf("❌ Failed to create collection %s: %v", collectionName, err)
74+
log.Printf("❌ Failed to create collection %s: %v", collectionName, err)
7575
}
7676

7777
log.Printf("✅ Created collection: %s", collectionName)
@@ -92,7 +92,7 @@ func CreateCollections(dbName string, collections []string) {
9292
log.Printf("List of Collections: %s", existingCollections)
9393

9494
if err != nil {
95-
log.Fatalf("Failed to list collections in DB %s: %v", dbName, err)
95+
log.Printf("Failed to list collections in DB %s: %v", dbName, err)
9696
}
9797

9898
existingMap := make(map[string]bool)
@@ -107,7 +107,7 @@ func CreateCollections(dbName string, collections []string) {
107107
for _, collection := range collections {
108108
if !existingMap[collection] {
109109
if err := db.CreateCollection(ctx, collection); err != nil {
110-
log.Fatalf("Failed to create collection %s: %v", collection, err)
110+
log.Printf("Failed to create collection %s: %v", collection, err)
111111
}
112112

113113
log.Printf("Created collection: %s", collection)
@@ -122,8 +122,8 @@ func CreateIndex(dbName string, collectionName string, indexModel mongo.IndexMod
122122
_, err := GetDb(dbName).Collection(collectionName).Indexes().CreateOne(ctx, indexModel)
123123

124124
if err != nil {
125-
log.Fatalf("❌ Failed to create index for collection: %s in db: %s", collectionName, dbName)
126-
log.Fatalf("❌ Error while creating creating index: %v", err)
125+
log.Printf("❌ Failed to create index for collection: %s in db: %s", collectionName, dbName)
126+
log.Printf("❌ Error while creating creating index: %v", err)
127127
} else {
128128
log.Println("✅ Index Created Successfully")
129129
}
@@ -141,8 +141,8 @@ func CreateUniqueIndex(dbName string, collectionName string, field string) {
141141
_, err := GetDb(dbName).Collection(collectionName).Indexes().CreateOne(ctx, indexModel)
142142

143143
if err != nil {
144-
log.Fatalf("❌ Failed to create index for collection: %s in db: %s", collectionName, dbName)
145-
log.Fatalf("❌ Error while creating creating index: %v", err)
144+
log.Printf("❌ Failed to create index for collection: %s in db: %s", collectionName, dbName)
145+
log.Printf("❌ Error while creating creating index: %v", err)
146146
} else {
147147
log.Println("✅ Index Created Successfully")
148148
}

0 commit comments

Comments
 (0)