【发布时间】:2022-01-27 12:40:40
【问题描述】:
以下是从提供集合、过滤查询、排序查询和限制数量的数据库中获取结果的代码。
func DBFetch(collection *mongo.Collection, filter interface{}, sort interface{}, limit int64) ([]bson.M, error) {
findOptions := options.Find()
findOptions.SetLimit(limit)
findOptions.SetSort(sort)
cursor, err := collection.Find(context.Background(), filter, findOptions)
var result []bson.M
if err != nil {
logger.Client().Error(err.Error())
sentry.CaptureException(err)
cursor.Close(context.Background())
return nil, err
}
if err = cursor.All(context.Background(), &result); err != nil {
logger.Client().Error(err.Error())
sentry.CaptureMessage(err.Error())
return nil, err
}
return result, nil
}
- 我正在使用 mongo-go 驱动程序版本 1.8.2
- mongodb community version 4.4.7 sharded mongo with 2 shards
- 每个分片在 k8 中有 30 个 CPU,245Gb 内存有 1 个副本
- API 为 200 rpm
- Api 从 mongo 获取数据并对其进行格式化并提供服务
- 我们在小学读和写。
- 大约每小时发生大量写入。
- 以毫秒为单位获取超时(大约 10ms-20ms)
【问题讨论】:
-
这似乎已经在这里详细回答了:stackoverflow.com/questions/24199729/…