Loading workspace environment...
Get started with MongoDB syntax, structure, and writing your first program.
// MongoDB 7.0 (MQL Query Syntax Sandbox)
db.orders.aggregate([
// Step 1: Match completed orders in 2024
{
$match: {
status: "completed",
orderDate: { $gte: ISODate("2024-01-01") }
}
},
// Step 2: Group by customer and compute total spend
{
$group: {
_id: "$customerId",
totalSpent: { $sum: "$amount" },
totalOrders: { $count: {} }
}
},
// Step 3: Sort by highest spend
{ $sort: { totalSpent: -1 } },
{ $limit: 10 }
]);