面试题答案
一键面试db.collection.aggregate([
{
$project: {
product: 1,
month: { $month: "$date" },
totalSales: { $multiply: ["$price", "$quantity"] }
}
},
{
$group: {
_id: {
product: "$product",
month: "$month"
},
totalSales: { $sum: "$totalSales" }
}
},
{
$match: {
totalSales: { $gt: 1000 }
}
},
{
$sort: {
"_id.month": 1,
"_id.product": 1
}
}
]);