mongodb中的类别层次结构

时间:2019-05-14 11:25:38

标签: mongodb aggregation-framework

我的文档结构如下:

[
    {
      "_id": ObjectId("54d81827e4a4449d023b4e34"),
      "name": "Refridgerator",
      "parent": null,
      "slug": "refridgerator"
    },
    {
        "_id": ObjectId("54d818227e4a4449d023b4e34"),
        "name": "Generator",
        "parent": null,
        "slug": "generator",
    },
    {
        "_id": ObjectId("54dc38bcse4a4449d023b4e58"),
        "name": "Bolt",
        "slug": "bolt",
        "parent": ObjectId("54d818227e4a4449d023b4e34")
    },
    {
      "_id": ObjectId("54dc38bce4a4449d023b4e58"),
      "name": "Ice Cream",
      "slug": "ice-cream",
      "parent": ObjectId("54d81827e4a4449d023b4e34")
    },
    {
      "_id": ObjectId("54dc3705e4a4449d023b4e56"),
      "name": "Chocolate",
      "slug": "chocolate",
      "parent": ObjectId("54d81827e4a4449d023b4e34")
    },
    {
        "_id": ObjectId("54dc38bce4a4449d023b4e68"),
        "name": "Mango Cream",
        "slug": "mango-cream",
        "parent": ObjectId("54dc38bce4a4449d023b4e58")
    },
    {
        "_id": ObjectId("54dc38bc74a4449d023b4e68"),
        "name": "Mango Cream Cream",
        "slug": "mango-cream-cream",
        "parent": ObjectId("54dc38bce4a4449d023b4e68")
    },
  ]

我正在使用mongodb及其类似的父子关系建立类别层次结构。类别为4级。

现在,我希望查询_id ='54d81827e4a4449d023b4e34',并且应该获取所有子类别。

我无法获取具有父子关系的json。

在上一个问题中,不会输出所需的结果。

预期输出:

    [
    {
      "_id": ObjectId("54d81827e4a4449d023b4e34"),
      "name": "Refridgerator",
      "parent": null,
      "slug": "refridgerator",
      "subCategory": [
        {
          "_id": ObjectId("54dc3705e4a4449d023b4e56"),
          "name": "Chocolate",
          "parent": ObjectId("54d81827e4a4449d023b4e34"),
          "slug": "chocolate"
        },
        {
          "_id": ObjectId("54dc38bce4a4449d023b4e58"),
          "name": "Ice Cream",
          "parent": ObjectId("54d81827e4a4449d023b4e34"),
          "slug": "ice-cream",
          'subsubcategory':[
            {
                "_id": ObjectId("54dc38bce4a4449d023b4e68"),
                "name": "Mango Cream",
                "slug": "mango-cream",
                "parent": ObjectId("54dc38bce4a4449d023b4e58"),
                'subsubsubcategory':[
                    {
                        "_id": ObjectId("54dc38bc74a4449d023b4e68"),
                        "name": "Mango Cream Cream",
                        "slug": "mango-cream-cream",
                        "parent": ObjectId("54dc38bce4a4449d023b4e68")
                    },
                  ]
            }
          ]
        }
      ]
    },
    {
        "_id": ObjectId("54d818227e4a4449d023b4e34"),
        "name": "Generator",
        "parent": null,
        "slug": "generator",
        "subCategory": [
          {
            "_id": ObjectId("54dc38bcse4a4449d023b4e58"),
            "name": "Bolt",
            "slug": "bolt",
            "parent": ObjectId("54d818227e4a4449d023b4e34")
          },
        ]
    }
  ]

0 个答案:

没有答案