From 4df2eab8764500b1aafc7762377e4d913b58f1fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 20 Sep 2022 15:10:54 +0800 Subject: [PATCH] - update mongo --- api.go | 3 +++ api_mongo.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/api.go b/api.go index 73a66e1..3e8458e 100644 --- a/api.go +++ b/api.go @@ -115,6 +115,9 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) { // 创建时间序列集合 c.mongoCreateCollection(ctx) + // 创建索引 + c.mongoCreateIndexes(ctx) + c.mongoConfig.stats = true } diff --git a/api_mongo.go b/api_mongo.go index 4d855d0..5bcfb8e 100644 --- a/api_mongo.go +++ b/api_mongo.go @@ -47,6 +47,21 @@ func (c *ApiClient) mongoCreateCollection(ctx context.Context) { } } +// 创建索引 +func (c *ApiClient) mongoCreateIndexes(ctx context.Context) { + indexes, err := c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).CreateManyIndexes(ctx, []mongo.IndexModel{ + { + Keys: bson.D{{ + Key: "log_time", + Value: -1, + }}, + }}) + if err != nil { + c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建索引:%s", err) + } + c.zapLog.WithTraceId(ctx).Sugar().Infof("创建索引:%s", indexes) +} + // 记录日志 func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongolLog) (err error) {