首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套层次上的猫鼬分类

嵌套层次上的猫鼬分类
EN

Stack Overflow用户
提问于 2021-11-10 15:42:57
回答 1查看 198关注 0票数 0

大家好,我是节点js社区的新手。

我正面临一个问题,与第二级过滤的类别。请帮助我如何过滤二级分组。

我的原始数据如下所示

代码语言:javascript
复制
Clothing
    Shoe    
        PUMA
Clothing
    Shoe    
        NIKE
Clothing
    Watches 
        TIMEX
Electronics
    Laptop
        DELL
Electronics
    Monitor
        HP

我在寻找像这样的

代码语言:javascript
复制
Clothing
    Shoe    
        PUMA    
        NIKE
    Watches 
        TIMEX
        
Electronics
    Laptop
        DELL
    Monitor
        HP

我的JSON如下所示,如何分组才能得到上述结果

代码语言:javascript
复制
{
    "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"
                }
            }
        }
    ]
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-28 20:03:01

你可以这样做:

代码语言:javascript
复制
Collection
  .find({},"collectionName"))
   .populate({
      path: "categories",
      model:"Category",
      select:"categoryName subCategories",
      populate:{
        path:"products",
        model:"Product",
      }
   }).then((products)=>{
    res.status(200).json(products);
   });
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69916186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档