db.sales.aggregate([
{
$unwind: "$transactions"
},
{
$match: {
"transactions.date": {
$gte: new ISODate("2023-01-01"),
$lt: new ISODate("2023-02-01")
}
}
},
{
$group: {
_id: {
customerId: "$transactions.customer.customerId",
customerName: "$transactions.customer.customerName"
},
totalAmount: {
$sum: "$transactions.amount"
}
}
},
{
$match: {
totalAmount: {
$gt: 100
}
}
},
{
$sort: {
totalAmount: -1
}
},
{
$project: {
_id: 0,
customerId: "$_id.customerId",
customerName: "$_id.customerName",
totalAmount: 1
}
}
]);