获取配置文件代码优化
This commit is contained in:
@@ -20,8 +20,7 @@ var db = new(mongo.Database)
|
||||
func init() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
link, _ := g.Cfg().Get(context.Background(), "mongo.address")
|
||||
mongoAddr := link.String()
|
||||
mongoAddr := g.Cfg().MustGet(context.Background(), "mongo.address").String()
|
||||
opt := options.Client().ApplyURI(mongoAddr)
|
||||
client, err := mongo.Connect(opt)
|
||||
if err != nil {
|
||||
@@ -95,9 +94,9 @@ func Insert(ctx context.Context, documents []interface{}, collection string, opt
|
||||
return
|
||||
}
|
||||
|
||||
// Count 查询总数
|
||||
// Count 查询总数
|
||||
func Count(ctx context.Context, filter bson.M, collection string) (count int64, err error) {
|
||||
// 调用驱动的 CountDocuments,在数据库端执行的
|
||||
count, err = db.Collection(collection).CountDocuments(ctx, filter)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user