当前位置: 首页 > news >正文

MongoDB聚合与查询优化详解

MongoDB聚合与查询优化详解

1. MongoDB聚合框架

1.1 聚合管道基础

// 使用mongo-driver pipeline := mongo.Pipeline{ {{Key: "$match", Value: bson.D{{Key: "status", Value: "active"}}}}, {{Key: "$group", Value: bson.D{ {Key: "_id", Value: "$department"}, {Key: "count", Value: bson.D{{Key: "$sum", Value: 1}}}, {Key: "avgSalary", Value: bson.D{{Key: "$avg", Value: "$salary"}}}, }}}, {{Key: "$sort", Value: bson.D{{Key: "count", Value: -1}}}}, } cursor, err := collection.Aggregate(ctx, pipeline)

1.2 常用聚合阶段

-- $match: 过滤文档 { $match: { status: "active", age: { $gte: 18 } } } -- $group: 分组 { $group: { _id: "$department", total: { $sum: 1 } } } -- $sort: 排序 { $sort: { total: -1 } } -- $limit: 限制数量 { $limit: 10 } -- $skip: 跳过文档 { $skip: 5 } -- $project: 选择字段 { $project: { name: 1, age: 1, _id: 0 } } -- $lookup: 左连接 { $lookup: { from: "orders", localField: "user_id", foreignField: "user_id", as: "user_orders" } }

2. 聚合表达式

2.1 算术表达式

// $add, $subtract, $multiply, $divide, $mod, $pow pipeline := mongo.Pipeline{ {{Key: "$addFields", Value: bson.D{ {Key: "totalPrice", Value: bson.D{{Key: "$multiply", Value: bson.A{"$price", "$quantity"}}}}, }}}, }

2.2 字符串表达式

// $concat, $toUpper, $toLower, $substr, $trim pipeline := mongo.Pipeline{ {{Key: "$addFields", Value: bson.D{ {Key: "fullName", Value: bson.D{{Key: "$concat", Value: bson.A{"$firstName", " ", "$lastName"}}}}, }}}, }

2.3 条件表达式

// $cond, $ifNull, $switch pipeline := mongo.Pipeline{ {{Key: "$addFields", Value: bson.D{ {Key: "status", Value: bson.D{ {Key: "$cond", Value: bson.A{ bson.D{{Key: "$gte", Value: bson.A{"$score", 60}}}, "pass", "fail", }}, }}, }}}, }

3. 查询优化

3.1 索引使用分析

// 使用explain分析查询 explain, err := collection.Find(ctx, filter).Explain(ctx) // 查看查询计划 cursor, err := collection.Find(ctx, filter, options.Find().SetHint("status_1"))

3.2 覆盖索引

// 创建覆盖索引 indexModel := mongo.IndexModel{ Keys: bson.D{ {Key: "user_id", Value: 1}, {Key: "name", Value: 1}, }, Options: options.Index().SetProjection(bson.D{{Key: "_id", Value: 0}}), } collection.Indexes().CreateOne(ctx, indexModel) // 查询只使用索引 cursor, err := collection.Find(ctx, bson.D{{Key: "user_id", Value: 1}}, options.Find().SetProjection(bson.D{{Key: "name", Value: 1}})

3.3 慢查询分析

// 查看慢查询 result := mongo.Database("admin").RunCommand(ctx, bson.D{{Key: "profile", Value: 2}}) // 设置慢查询阈值 mongo.Database("admin").RunCommand(ctx, bson.D{ {Key: "setParameter", Value: 1}, {Key: "slowms", Value: 100}, })

4. 查询模式

4.1 分页查询

// 简单分页 skip := int64((page - 1) * pageSize) limit := int64(pageSize) cursor, err := collection.Find(ctx, filter, options.Find().SetSkip(skip).SetLimit(limit).SetSort(bson.D{{Key: "created_at", Value: -1}})) // 游标分页(适合大数据量) var lastID primitive.ObjectID cursor, err := collection.Find(ctx, bson.D{ {Key: "_id", Value: bson.D{{Key: "$gt", Value: lastID}}}, {Key: "status", Value: "active"}, })

4.2 模糊查询

// 使用正则 cursor, err := collection.Find(ctx, bson.D{ {Key: "name", Value: bson.D{{Key: "$regex", Value: "john"}}}, }) // 前缀匹配(可使用索引) cursor, err := collection.Find(ctx, bson.D{ {Key: "name", Value: bson.D{{Key: "$regex", Value: "^john"}}}, })

4.3 数组查询

// 查询数组包含 cursor, err := collection.Find(ctx, bson.D{ {Key: "tags", Value: "mongodb"}}, ) // 查询数组包含所有元素 cursor, err := collection.Find(ctx, bson.D{ {Key: "tags", Value: bson.D{{Key: "$all", Value: bson.A{"mongodb", "database"}}}}, }) // 按数组元素查询 cursor, err := collection.Find(ctx, bson.D{ {Key: "ratings.0.stars", Value: 5}}, )

5. 索引优化

5.1 复合索引

// 创建复合索引 indexModel := mongo.IndexModel{ Keys: bson.D{ {Key: "user_id", Value: 1}, {Key: "created_at", Value: -1}, {Key: "status", Value: 1}, }, } collection.Indexes().CreateOne(ctx, indexModel)

5.2 多键索引

// 对数组字段创建索引 indexModel := mongo.IndexModel{ Keys: bson.D{{Key: "tags", Value: 1}}, } collection.Indexes().CreateOne(ctx, indexModel) // 对嵌套数组创建索引 indexModel := mongo.IndexModel{ Keys: bson.D{{Key: "reviews.rating", Value: 1}}, } collection.Indexes().CreateOne(ctx, indexModel)

5.3 索引管理

// 查看所有索引 indexes, err := collection.Indexes().List(ctx) // 创建索引 _, err = collection.Indexes().CreateOne(ctx, mongo.IndexModel{ Keys: bson.D{{Key: "name", Value: 1}}, Options: options.Index().SetUnique(true), }) // 删除索引 collection.Indexes().DropOne(ctx, "name_1") // 查看索引大小 var stats bson.M collection.Database().RunCommand(ctx, bson.D{ {Key: "indexStats", Value: collection.Name()}, }, &stats)

6. 性能最佳实践

6.1 查询优化原则

  • 使用覆盖索引避免回表
  • 避免全表扫描
  • 合理使用投影减少传输数据
  • 使用分页处理大数据集
  • 避免使用$where和$function

6.2 写入优化

// 批量写入 models := make([]mongo.WriteModel, len(docs)) for i, doc := range docs { models[i] = mongo.NewInsertOneModel().SetDocument(doc) } result, err := collection.BulkWrite(ctx, models) // 顺序写入比随机写入快

6.3 连接池配置

clientOptions := options.Client(). ApplyURI("mongodb://localhost:27017"). SetMaxPoolSize(100). SetMinPoolSize(10). SetMaxConnIdleTime(30 * time.Second) client, err := mongo.Connect(ctx, clientOptions)

7. 总结

MongoDB聚合框架提供了强大的数据处理能力,通过合理的管道设计可以实现复杂的查询和分析。查询优化需要关注索引设计、查询模式和写入策略,通过explain分析和慢查询监控持续改进性能。

http://www.jsqmd.com/news/792173/

相关文章:

  • 如何在 Docker 容器中部署企业微信机器人服务保证高可用
  • 31_AI短片实战第四弹:主观视角空间控制与分屏快速剪辑的AI生成策略(附提示词)
  • 高管求职渠道公司实测:4家机构核心能力对比评测 - 得赢
  • 两次全球宕机之后,Cloudflare 用半年时间重建了什么
  • 2026届最火的AI写作平台推荐榜单
  • Logseq AI助手插件:在知识管理笔记中集成ChatGPT智能写作与编辑
  • hls::stream<ap_uint<DW * NPPC>> src,报错原因分析
  • 32_AI短片实战第五弹:飞跃峡谷——高潮镜头的“放手”哲学与首帧脑补策略(附提示词)
  • DeepSeek V4 横向对比真实表现
  • 终极指南:如何用NPYViewer快速查看和可视化NumPy数组数据
  • YOLO11进阶技巧:数据增强策略 | 舍弃传统Mosaic,引入Copy-Paste与MixUp混合数据增强,有效缓解过拟合
  • R7000P梅林固件进阶玩法:解锁软件中心、挂载U盘与插件安装全攻略
  • 告别数据丢失焦虑:用Python手把手实现Reed-Solomon码(附完整代码)
  • 避开Verilog状态机设计里的那些‘坑’:从HDLbits的Fsm hdlc题看帧同步错误处理
  • 2026年4月优质的vi设计团队推荐,山野风餐饮设计/连锁餐饮品牌设计/连锁餐厅品牌设计,vi设计团队选哪家 - 品牌推荐师
  • 2026最权威的六大AI写作平台解析与推荐
  • LinkSwift:九大网盘直链解析工具使用指南
  • HDLbits进阶实战:FSM与移位寄存器在复杂时序电路中的四种设计范式
  • 三步永久保存微信聊天记录的完整指南:告别数据丢失的烦恼
  • 2026届最火的六大降AI率网站实测分析
  • 终极Notero使用指南:如何快速实现Zotero与Notion文献同步
  • 避开这3个坑,你的PMSM滑模观测器仿真结果才能和论文里一样准
  • APIO2026 打铁记
  • 2026年4月市面上比较好的喷墨机供应厂家推荐,水墨数码机/数码机/数码打印机/扫描机/打样机/直出机,喷墨机企业推荐 - 品牌推荐师
  • OpenClaw.NET 外部 CLI 连接器 (External CLI Connectors) 详细技术总结
  • 智能车信标FM信号不稳?用9018和UPC1677搭建射频功放的避坑指南
  • S7-1200 PLC RS232自由口PTP通信实战:从硬件组态到数据收发
  • 三菱Q系列PLC CC-Link远程IO站配置与诊断实战
  • 2026年南京有实力的鹅卵石工厂推荐,黑灰色砾石/儿童乐园石英砂/景观砾石/鹅卵石滤料,鹅卵石批发厂家哪家好 - 品牌推荐师
  • CSS 阴影高级技巧完全指南