获取配置文件代码优化

This commit is contained in:
2025-11-28 14:01:08 +08:00
parent ab7d3de666
commit 7e2884414d
5 changed files with 13 additions and 23 deletions

View File

@@ -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
}
}