| 语法 | { $facet: { 名A: [子管道], 名B: [子管道] } } |
|---|---|
| 限制 | $facet 必须是较后阶段,子管道内不能再出现 $facet/$out 等 |
示例
示例
db.orders.aggregate([
{ $match: { ts: { $gte: ISODate("2026-10-01T00:00:00Z") } } },
{ $facet: {
byStatus: [ { $group: { _id: "$status", n: { $sum: 1 } } } ],
topSkus: [ { $group: { _id: "$sku", qty: { $sum: "$qty" } } }, { $sort: { qty: -1 } }, { $limit: 5 } ]
} }
])子管道相互独立:各自从头处理同一批输入,注意整体内存开销。