我在 mongodb 中具有这样的收藏集。
[
{
"_id": 1, "name": "drinks", isActive: 1
}, {
"_id": 2, "name": "coke", isActive: 1, categoryId: 1
}, {
"_id": 3, "name": "fanta", isActive: 1, categoryId: 1
}, {
"_id": 4, "name": "cloths", isActive: 1
}
]
在此集合中,如果存在“ categoryId”,则表示它不是主要类别,而是子类别 所以我想列出类别下的所有子类别,并且我想要在nodejs中这样的结果 猫鼬
我需要的输出
[
{
_id: 1,
name: "drinks",
isActive: 1,
subcategories: [{ "_id": 3, "name": "fanta", isActive: 1, categoryId: 1 },
{ "_id": 2, "name": "coke", isActive: 1, categoryId: 1 }],
}, {
"_id": 4, "name": "cloths", isActive: 1, subcategories: []
}
]