MongoDB $sort
Reorder documents by one or more fields.
Use `1` for ascending order and `-1` for descending order. Put it before `$limit` when you need the highest or lowest results from the full stream.
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 03, 07, 08, 11, 12, and Hard Mode 13.
Example pipeline syntax { $sort: { total: -1, createdAt: 1 } }
Continue learning Challenge 03: Rank the largest orders Return the three highest-value orders, largest first. Challenge 04: Filter a priority web queue Return completed web orders worth at least 150. Shape each result to `orderId`, `region`, and `total`, then sort largest first. Challenge 05: Build a store dispatch queue Find completed store orders, select the two lowest-value orders, then return `orderId`, `customerId`, and `total` in ascending total order. Challenge 11: Roll up regional revenue For completed orders, return revenue and order count by region, highest revenue first. 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 15: Find repeat customers For completed orders, find customers with at least two orders. Return `customerId`, `orderCount`, and `revenue`, sorted by revenue descending. Challenge 17: Run parallel analyses For completed orders, return `byChannel` revenue summaries and the top two `byRegion` summaries in one document. 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 22: Build regional running revenue For completed orders, number each order within its region by date and calculate running regional revenue. Return `orderId`, `region`, `placedAt`, `regionalOrderNumber`, and `runningRevenue`, ordered by region and date. Challenge 23: Trace the category hierarchy For every leaf category, traverse its `parentId` chain through the `categories` collection. Return `category`, `parentId`, and `ancestorDepth`, sorted by category name. Challenge 24: Roll up a monthly revenue series For completed orders, calculate monthly revenue and order count. Return `month`, `revenue`, and `orderCount` in chronological order. 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 $sort documentation Browse all aggregation operators