01 基本概念

什么是聚合框架

用于一个或几个几个集合上进行的一系列运算从而得到期望的数据集的运算框架

类似:group by、left join

管道(Pipeline)

  • 聚合框架对数据进行处理的过程
  • 与linux管道有类似之处

步骤 Stage

  • 管道中的一步操作。

02 具体使用

为什么只有左外连接?

  • 原因一:反范式设计;
  • 原因二:读取效率低下
  • 原因三:分布式环境

03 进阶使用

1
2
3
4
db.orders.aggregate([
{ $match: { status: "A" } },
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }
])

First Stage: The $match stage filters the documents by the status field and passes to the next stage those documents that have status equal to "A".

Second Stage: The $group stage groups the documents by the cust_id field to calculate the sum of the amount for each unique cust_id.

帮助文档

CRUD

aggregation

Aggregation

训练营答疑

答疑汇总