MongoDB $unwind
Turn each array element into its own output document.
After unwinding `items`, each output document contains one item instead of the whole items array. The surrounding order fields are repeated on every emitted document.
Stages are the top-level steps in a pipeline. Each stage receives documents, changes the stream in one specific way, and passes its output to the next stage.
Course use: Exercises 05, 10, 12, and Hard Mode 13.
Example pipeline syntax { $unwind: "$items" }
Continue learning Challenge 07: Open every line item Create one row per line item with `orderId`, `productId`, `qty`, and `unitPrice`. Challenge 14: Measure product demand Using completed orders, return `productId`, total `units`, and `revenue` for every product. Sort by revenue descending, then product ID ascending. Challenge 16: Attach customer context Attach each customer, then return `orderId`, `customerName`, `segment`, and `total`. Challenge 18: Map regional category leaders Using completed orders, calculate line revenue, join product categories, total revenue by region and category, then return every region-category pair ranked by revenue. Challenge 19: Rank customer lifetime leaders For completed orders, return the top three customers by revenue. Include `customerId`, `customerName`, `segment`, `revenue`, `orderCount`, and `averageOrder`. Challenge 20: Assemble an operations dashboard For completed orders, return one document with `regionalRevenue`, `largestOrders`, and `unitsByChannel`. Regional summaries need revenue and order count; largest orders need the top three compact order records; channel summaries need item units. Challenge 21: Build the commerce command report For completed orders, return one document with three arrays. `categoryPerformance` contains the top three categories with `category`, `revenue`, and `units`. `segmentPerformance` contains every customer segment with `segment`, `revenue`, `orderCount`, and `averageOrder`. `topOrder` contains the largest order with `orderId`, `region`, and `total`. Sort every report by revenue or total descending, using its name as the tie-breaker. Challenge 25: Tune a lean category report For completed web orders, report revenue by region and product category. `$match` must run first, `$project` must trim documents before `$lookup`, and the complete pipeline must use no more than nine stages. Official MongoDB $unwind documentation Browse all aggregation operators