0

My document structure is as follows:

[
    {
      "_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")
    },
  ]

I’m making a category hierarchy using mongodb and its like parent-child relationship. Category is of 4 level.

Now I wish to query for _id = ‘54d81827e4a4449d023b4e34’ and should get back all the child categories.

I’m unable to get the json structured with parent – child relations.

In previous asked question does not output the required result.

Expected Output:

    [
    {
      "_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")
          },
        ]
    }
  ]
Vaibhav Agarwal
  • 238
  • 5
  • 19
  • what is the expected output of such query ? – mickl May 14 '19 at 11:39
  • @mickl: I have added the expected output. Please check and let me know if you want anything else. – Vaibhav Agarwal May 14 '19 at 11:59
  • the problem is that `parent` is of type `string` while `_id` is an `ObjectId`. Is it possible that you can update your db and make them of the same type before running this aggregation ? – mickl May 14 '19 at 12:13
  • Actually, when parent is null this means this is parent category and when slug matches with any parent that means this is sub category of matching slug. – Vaibhav Agarwal May 14 '19 at 12:28
  • I know but if you want to match them somehow they need to share the same type since MongoDB compares types before values, is that possible ? – mickl May 14 '19 at 12:58
  • Okay we can make parent of type ObjectId and store _id field in parent in place of slug. – Vaibhav Agarwal May 14 '19 at 13:12
  • 1
    I've deleted my answer, here's what you need: https://stackoverflow.com/questions/52433933/hierarchical-queries-with-mongo-using-graphlookup/52662426#52662426 – mickl May 14 '19 at 15:39
  • @mickl: It works great but a little problem, if I want to fetch all top level employees and want to add more level then what changes I need to do in that? – Vaibhav Agarwal May 15 '19 at 04:24

0 Answers0