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) {