大家好,我是节点js社区的新手。
我正面临一个问题,与第二级过滤的类别。请帮助我如何过滤二级分组。
我的原始数据如下所示
Clothing
Shoe
PUMA
Clothing
Shoe
NIKE
Clothing
Watches
TIMEX
Electronics
Laptop
DELL
Electronics
Monitor
HP我在寻找像这样的
Clothing
Shoe
PUMA
NIKE
Watches
TIMEX
Electronics
Laptop
DELL
Monitor
HP我的JSON如下所示,如何分组才能得到上述结果
{
"success": true,
"data": [
{
"_id": "618a094d36143c2c807f0124",
"parentId": null,
"title": "Clothing",
"level2": {
"_id": "618a098636143c2c807f0129",
"parentId": "618a094d36143c2c807f0124",
"title": "Shoe",
"level3": {
"_id": "618a09ba36143c2c807f012e",
"parentId": "618a098636143c2c807f0129",
"title": "PUMA"
}
}
},
{
"_id": "618a094d36143c2c807f0124",
"parentId": null,
"title": "Clothing",
"level2": {
"_id": "618a098636143c2c807f0129",
"parentId": "618a094d36143c2c807f0124",
"title": "Shoe",
"level3": {
"_id": "618b820ebd59b4c554d880f3",
"parentId": "618a098636143c2c807f0129",
"title": "NIKE"
}
}
},
{
"_id": "618a094d36143c2c807f0124",
"parentId": null,
"title": "Clothing",
"level2": {
"_id": "618a099136143c2c807f012b",
"parentId": "618a094d36143c2c807f0124",
"title": "Watches",
"level3": {
"_id": "618a09d436143c2c807f0130",
"parentId": "618a099136143c2c807f012b",
"title": "TIMEX"
}
}
},
{
"_id": "618a095f36143c2c807f0126",
"parentId": null,
"title": "Electronics",
"level2": {
"_id": "618a09ed36143c2c807f0132",
"parentId": "618a095f36143c2c807f0126",
"title": "Laptop",
"level3": {
"_id": "618a0a0336143c2c807f0134",
"parentId": "618a09ed36143c2c807f0132",
"title": "Dell"
}
}
},
{
"_id": "618a095f36143c2c807f0126",
"parentId": null,
"title": "Electronics",
"level2": {
"_id": "618a0a2136143c2c807f0136",
"parentId": "618a095f36143c2c807f0126",
"title": "Monitor",
"level3": {
"_id": "618a0a2e36143c2c807f0138",
"parentId": "618a0a2136143c2c807f0136",
"title": "HP"
}
}
}
]
}发布于 2022-06-28 20:03:01
你可以这样做:
Collection
.find({},"collectionName"))
.populate({
path: "categories",
model:"Category",
select:"categoryName subCategories",
populate:{
path:"products",
model:"Product",
}
}).then((products)=>{
res.status(200).json(products);
});
} https://stackoverflow.com/questions/69916186
复制相似问题