Aggregation Pipeline

データのベルトコンベア。フィルタリング、加工、集計をステップごとに実行する。

Pipeline Stages

aggregation.js
db.orders.aggregate([
// Stage 1: Filter
{ $match: { status: "completed" } },
// Stage 2: Group by User & Sum total
{ $group: {
_id: "$user_id",
totalSpent: { $sum: "$amount" }
} },
// Stage 3: Sort by spender
{ $sort: { totalSpent: -1 } }
]);
  • $match: フィルタリング(SQLのWHERE)
  • $group: グルーピング(SQLのGROUP BY)
  • $lookup: コレクション結合(SQLのLEFT JOIN)