forked from hwholiday/learning_tools
/
mongodb.go
29 lines (26 loc) · 909 Bytes
/
mongodb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package db
import (
"context"
"file_storage/base/config"
"file_storage/base/tool"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
func initMongoDb() {
//链接mongo服务
opt := options.Client().ApplyURI(config.GetMgoConfig().GetUrl())
opt.SetLocalThreshold(3 * time.Second) //只使用与mongo操作耗时小于3秒的
if config.GetMgoConfig().GetName() != "" && config.GetMgoConfig().GetPass() != "" {
opt.SetAuth(options.Credential{
Username: config.GetMgoConfig().GetName(),
Password: config.GetMgoConfig().GetPass(),
})
}
opt.SetMaxConnIdleTime(5 * time.Second) //指定连接可以保持空闲的最大毫秒数
opt.SetMaxPoolSize(200) //使用最大的连接数
if mgo, err = mongo.Connect(context.TODO(), opt); err != nil {
panic(err)
}
tool.GetLogger().Debug("mgo success : " + config.GetMgoConfig().GetUrl())
}