Files
crawlab/db/mongo/db.go
2024-11-22 20:44:26 +08:00

25 lines
455 B
Go

package mongo
import (
"github.com/apex/log"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/mongo"
)
func GetMongoDb(dbName string) *mongo.Database {
// Use default database name if not provided
if dbName == "" {
if dbName = viper.GetString("mongo.db"); dbName == "" {
dbName = "test"
}
}
c, err := GetMongoClient()
if err != nil {
log.Errorf("error getting mongo client: %v", err)
return nil
}
return c.Database(dbName)
}