面试题答案
一键面试db.orders.aggregate([
{
$unwind: "$order_items"
},
{
$match: {
"order_items.product_name": "laptop",
"order_items.quantity": { $gt: 5 }
}
},
{
$group: {
_id: "$customer_id",
totalAmount: {
$sum: {
$multiply: ["$order_items.quantity", "$order_items.price"]
}
}
}
},
{
$sort: {
totalAmount: -1
}
}
]);