mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-22 17:31:03 +01:00
36 lines
590 B
Go
36 lines
590 B
Go
package mongo
|
|
|
|
import (
|
|
"github.com/crawlab-team/crawlab/trace"
|
|
"github.com/spf13/viper"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
func GetMongoDb(dbName string, opts ...DbOption) (db *mongo.Database) {
|
|
if dbName == "" {
|
|
dbName = viper.GetString("mongo.db")
|
|
}
|
|
if dbName == "" {
|
|
dbName = "test"
|
|
}
|
|
|
|
_opts := &DbOptions{}
|
|
for _, op := range opts {
|
|
op(_opts)
|
|
}
|
|
|
|
var c *mongo.Client
|
|
if _opts.client == nil {
|
|
var err error
|
|
c, err = GetMongoClient()
|
|
if err != nil {
|
|
trace.PrintError(err)
|
|
return nil
|
|
}
|
|
} else {
|
|
c = _opts.client
|
|
}
|
|
|
|
return c.Database(dbName, nil)
|
|
}
|