我有两个名为的集合 BlogCategory 和 SubBlogCategory .BlogCategory的文档包含子类别id的objectId数组。
类别文件如下
{
"_id" : ObjectId("5af2c6e8bfab7269e9a42ded"),
"title" : "Javascript",
"subcategory" : [
ObjectId("5af29fcc9a52623b7088ef4e"),
ObjectId("5aebf78681273424e5f55ecc")
]
}
子类别文件如下
{
"_id" : ObjectId("5af29fcc9a52623b7088ef4e"),
"title" : "Reactjs"
}
{
"_id" : ObjectId("5aebf78681273424e5f55ecc"),
"title" : "Vuejs",
}
如何在获取集合类别的所有文档时填充类别文档中的子类别文档?
我使用了以下查询,但它只填充了数组的单个id
db.BlogCategory.aggregate([
{
$unwind: "$subcategory"
},
{
$lookup:
{
from: "SubBlogCategory",
localField: "subcategory",
foreignField: "_id",
as: "sub_doc"
}
}
])