嗨,我有两个收款人和部门。这些集合具有以下属性。
我想聚合一个内部数组并将其添加到文档的根目录中。请在下面检查我需要的结果:
人:
{
_id:ObjectID("5ff93b43535bera64de4"),
first_name: some name,
last_name : some name,
dept: department1,
}
部:
{
dept_name: department1,
dept_descp : this is description,
books: [
{
book_name: book1,
subject: subject1,
},
{
book_name: book2,
subject: subject2,
},
]
}
到目前为止,我已经尝试过,但没有得到所需的结果
person.aggregate([
{
$match:
{
"_id": ObjectId("5ff93b43535bera64de4")
}
},
{
$lookup: {
from: "department",
localField: "dept",
foreignField: "dept_name",
as: "ndept"
}
},
{
$project:{"books": '$ndept.books'}
}
])
我想得到的结果是
结果:
{
_id:ObjectID("5ff93b43535bera64de4"),
first_name: some name,
last_name : some name,
dept: department1,
books: [
{
book_name: book1,
subject: subject1,
},
{
book_name: book2,
subject: subject2,
}
]
}