面试题答案
一键面试db.User.aggregate([
{
$lookup: {
from: "Order",
localField: "_id",
foreignField: "user_id",
as: "orders",
pipeline: [
{
$match: {
order_date: { $gte: new ISODate("2023-10-01T00:00:00Z") }
}
},
{
$lookup: {
from: "Product",
localField: "product_ids",
foreignField: "_id",
as: "products"
}
},
{
$project: {
_id: 1,
order_date: 1,
product_names: "$products.product_name"
}
}
]
}
},
{
$project: {
name: 1,
orders: 1
}
}
]);