0

I have a collection of Categories where a single category can have multiple parents categories or null in case of root category. The schema is as follows:

const categorySchema = mongoose.Schema({
    CategoryName: [{
        type: String,
        required: true
    }],
    CategoryText: {
        type: String,
        required: true
    },
    CategoryImage: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'multimedias'
    }],
    Parent: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'categories'
    }]
}, { timestamps: true });


module.exports = mongoose.model('categories', categorySchema);

I want the output to be something like:

[
  {
    "_id": "63fca1069d37c3c313576eb0",
    "CategoryName": [
      "sdfsdfdsfdsf"
    ],
    "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
    "CategoryImage": [
      "63fbe20ab7d2392dbf426edc",
      "63fbe129b7d2392dbf426eac",
      "63fbe0b5b7d2392dbf426e90"
    ],
    "Parent": [
      null
    ],
    "createdAt": "2023-02-27T12:24:38.910Z",
    "updatedAt": "2023-02-27T12:24:38.910Z",
    "__v": 0,
    "children": [
      
    ]
  },
  {
    "_id": "63fca21a9576bde3872ab882",
    "CategoryName": [
      "wqerweqrwer"
    ],
    "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
    "CategoryImage": [
      "63fbe20ab7d2392dbf426edc",
      "63fbe129b7d2392dbf426eac",
      "63fbe0b5b7d2392dbf426e90"
    ],
    "Parent": [
      null
    ],
    "createdAt": "2023-02-27T12:29:14.541Z",
    "updatedAt": "2023-02-27T12:29:14.541Z",
    "__v": 0,
    "children": [
      {
        "_id": "63fceef2f8f86c84b0a0c0c4",
        "CategoryName": [
          "Something for second one"
        ],
        "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
        "CategoryImage": [
          "63fbe20ab7d2392dbf426edc",
          "63fbe129b7d2392dbf426eac",
          "63fbe0b5b7d2392dbf426e90"
        ],
        "Parent": [
          "63fca21a9576bde3872ab882"
        ],
        "createdAt": "2023-02-27T12:30:25.869Z",
        "updatedAt": "2023-02-27T12:30:25.869Z",
        "__v": 0,
        "level": 0
      }
    ]
  },
  {
    "_id": "63fca2459576bde3872ab896",
    "CategoryName": [
      "Third category"
    ],
    "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
    "CategoryImage": [
      "63fbe20ab7d2392dbf426edc",
      "63fbe129b7d2392dbf426eac",
      "63fbe0b5b7d2392dbf426e90"
    ],
    "Parent": [
      null
    ],
    "createdAt": "2023-02-27T12:29:57.604Z",
    "updatedAt": "2023-02-27T12:29:57.604Z",
    "__v": 0,
    "children": [
      {
        "_id": "63fca551f8f86c84b0a0c0c1",
        "CategoryName": [
          "Child of 3rd one"
        ],
        "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
        "CategoryImage": [
          "63fbe20ab7d2392dbf426edc",
          "63fbe129b7d2392dbf426eac",
          "63fbe0b5b7d2392dbf426e90"
        ],
        "Parent": [
          "63fca2459576bde3872ab896"
        ],
        "Children": [
          {
            "_id": "63fca5def8f86c84b0a0c0c2",
            "CategoryName": [
              "Super Child of 3rd one"
            ],
            "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
            "CategoryImage": [
              "63fbe20ab7d2392dbf426edc",
              "63fbe129b7d2392dbf426eac",
              "63fbe0b5b7d2392dbf426e90"
            ],
            "Parent": [
              "63fca551f8f86c84b0a0c0c1"
            ],
            "createdAt": "2023-02-27T12:30:25.869Z",
            "updatedAt": "2023-02-27T12:30:25.869Z",
            "__v": 0,
            "level": 1
          }
        ],
        "createdAt": "2023-02-27T12:30:25.869Z",
        "updatedAt": "2023-02-27T12:30:25.869Z",
        "__v": 0,
        "level": 0
      }
    ]
  },
  {
    "_id": "63fca2619576bde3872ab8aa",
    "CategoryName": [
      "Fourth one"
    ],
    "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
    "CategoryImage": [
      "63fbe20ab7d2392dbf426edc",
      "63fbe129b7d2392dbf426eac",
      "63fbe0b5b7d2392dbf426e90"
    ],
    "Parent": [
      null
    ],
    "createdAt": "2023-02-27T12:30:25.869Z",
    "updatedAt": "2023-02-27T12:30:25.869Z",
    "__v": 0,
    "children": [
      
    ]
  }
]

But what now I'm getting is :

[
  {
    "_id": "63fca1069d37c3c313576eb0",
    "CategoryName": [
      "sdfsdfdsfdsf"
    ],
    "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
    "CategoryImage": [
      "63fbe20ab7d2392dbf426edc",
      "63fbe129b7d2392dbf426eac",
      "63fbe0b5b7d2392dbf426e90"
    ],
    "Parent": [
      null
    ],
    "createdAt": "2023-02-27T12:24:38.910Z",
    "updatedAt": "2023-02-27T12:24:38.910Z",
    "__v": 0,
    "children": [
      
    ]
  },
  {
    "_id": "63fca21a9576bde3872ab882",
    "CategoryName": [
      "wqerweqrwer"
    ],
    "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
    "CategoryImage": [
      "63fbe20ab7d2392dbf426edc",
      "63fbe129b7d2392dbf426eac",
      "63fbe0b5b7d2392dbf426e90"
    ],
    "Parent": [
      null
    ],
    "createdAt": "2023-02-27T12:29:14.541Z",
    "updatedAt": "2023-02-27T12:29:14.541Z",
    "__v": 0,
    "children": [
      {
        "_id": "63fceef2f8f86c84b0a0c0c4",
        "CategoryName": [
          "Something for second one"
        ],
        "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
        "CategoryImage": [
          "63fbe20ab7d2392dbf426edc",
          "63fbe129b7d2392dbf426eac",
          "63fbe0b5b7d2392dbf426e90"
        ],
        "Parent": [
          "63fca21a9576bde3872ab882"
        ],
        "createdAt": "2023-02-27T12:30:25.869Z",
        "updatedAt": "2023-02-27T12:30:25.869Z",
        "__v": 0,
        "level": 0
      }
    ]
  },
  {
    "_id": "63fca2459576bde3872ab896",
    "CategoryName": [
      "Third category"
    ],
    "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
    "CategoryImage": [
      "63fbe20ab7d2392dbf426edc",
      "63fbe129b7d2392dbf426eac",
      "63fbe0b5b7d2392dbf426e90"
    ],
    "Parent": [
      null
    ],
    "createdAt": "2023-02-27T12:29:57.604Z",
    "updatedAt": "2023-02-27T12:29:57.604Z",
    "__v": 0,
    "children": [
      {
        "_id": "63fca5def8f86c84b0a0c0c2",
        "CategoryName": [
          "Super Child of 3rd one"
        ],
        "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
        "CategoryImage": [
          "63fbe20ab7d2392dbf426edc",
          "63fbe129b7d2392dbf426eac",
          "63fbe0b5b7d2392dbf426e90"
        ],
        "Parent": [
          "63fca551f8f86c84b0a0c0c1"
        ],
        "createdAt": "2023-02-27T12:30:25.869Z",
        "updatedAt": "2023-02-27T12:30:25.869Z",
        "__v": 0,
        "level": 1
      },
      {
        "_id": "63fca551f8f86c84b0a0c0c1",
        "CategoryName": [
          "Child of 3rd one"
        ],
        "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
        "CategoryImage": [
          "63fbe20ab7d2392dbf426edc",
          "63fbe129b7d2392dbf426eac",
          "63fbe0b5b7d2392dbf426e90"
        ],
        "Parent": [
          "63fca2459576bde3872ab896"
        ],
        "createdAt": "2023-02-27T12:30:25.869Z",
        "updatedAt": "2023-02-27T12:30:25.869Z",
        "__v": 0,
        "level": 0
      }
    ]
  },
  {
    "_id": "63fca2619576bde3872ab8aa",
    "CategoryName": [
      "Fourth one"
    ],
    "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
    "CategoryImage": [
      "63fbe20ab7d2392dbf426edc",
      "63fbe129b7d2392dbf426eac",
      "63fbe0b5b7d2392dbf426e90"
    ],
    "Parent": [
      null
    ],
    "createdAt": "2023-02-27T12:30:25.869Z",
    "updatedAt": "2023-02-27T12:30:25.869Z",
    "__v": 0,
    "children": [
      
    ]
  }
]

Here is the extract of the data in the collection:

[{
  "_id": {
    "$oid": "63fca1069d37c3c313576eb0"
  },
  "CategoryName": [
    "sdfsdfdsfdsf"
  ],
  "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
  "CategoryImage": [
    {
      "$oid": "63fbe20ab7d2392dbf426edc"
    },
    {
      "$oid": "63fbe129b7d2392dbf426eac"
    },
    {
      "$oid": "63fbe0b5b7d2392dbf426e90"
    }
  ],
  "Parent": [
    null
  ],
  "createdAt": {
    "$date": {
      "$numberLong": "1677500678910"
    }
  },
  "updatedAt": {
    "$date": {
      "$numberLong": "1677500678910"
    }
  },
  "__v": 0
},{
  "_id": {
    "$oid": "63fca21a9576bde3872ab882"
  },
  "CategoryName": [
    "wqerweqrwer"
  ],
  "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
  "CategoryImage": [
    {
      "$oid": "63fbe20ab7d2392dbf426edc"
    },
    {
      "$oid": "63fbe129b7d2392dbf426eac"
    },
    {
      "$oid": "63fbe0b5b7d2392dbf426e90"
    }
  ],
  "Parent": [
    null
  ],
  "createdAt": {
    "$date": {
      "$numberLong": "1677500954541"
    }
  },
  "updatedAt": {
    "$date": {
      "$numberLong": "1677500954541"
    }
  },
  "__v": 0
},{
  "_id": {
    "$oid": "63fca2459576bde3872ab896"
  },
  "CategoryName": [
    "Third category"
  ],
  "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
  "CategoryImage": [
    {
      "$oid": "63fbe20ab7d2392dbf426edc"
    },
    {
      "$oid": "63fbe129b7d2392dbf426eac"
    },
    {
      "$oid": "63fbe0b5b7d2392dbf426e90"
    }
  ],
  "Parent": [
    null
  ],
  "createdAt": {
    "$date": {
      "$numberLong": "1677500997604"
    }
  },
  "updatedAt": {
    "$date": {
      "$numberLong": "1677500997604"
    }
  },
  "__v": 0
},{
  "_id": {
    "$oid": "63fca2619576bde3872ab8aa"
  },
  "CategoryName": [
    "Fourth one"
  ],
  "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
  "CategoryImage": [
    {
      "$oid": "63fbe20ab7d2392dbf426edc"
    },
    {
      "$oid": "63fbe129b7d2392dbf426eac"
    },
    {
      "$oid": "63fbe0b5b7d2392dbf426e90"
    }
  ],
  "Parent": [
    null
  ],
  "createdAt": {
    "$date": {
      "$numberLong": "1677501025869"
    }
  },
  "updatedAt": {
    "$date": {
      "$numberLong": "1677501025869"
    }
  },
  "__v": 0
},{
  "_id": {
    "$oid": "63fca551f8f86c84b0a0c0c1"
  },
  "CategoryName": [
    "Child of 3rd one"
  ],
  "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
  "CategoryImage": [
    {
      "$oid": "63fbe20ab7d2392dbf426edc"
    },
    {
      "$oid": "63fbe129b7d2392dbf426eac"
    },
    {
      "$oid": "63fbe0b5b7d2392dbf426e90"
    }
  ],
  "Parent": [
    {
      "$oid": "63fca2459576bde3872ab896"
    }
  ],
  "createdAt": {
    "$date": {
      "$numberLong": "1677501025869"
    }
  },
  "updatedAt": {
    "$date": {
      "$numberLong": "1677501025869"
    }
  },
  "__v": 0
},{
  "_id": {
    "$oid": "63fca5def8f86c84b0a0c0c2"
  },
  "CategoryName": [
    "Super Child of 3rd one"
  ],
  "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
  "CategoryImage": [
    {
      "$oid": "63fbe20ab7d2392dbf426edc"
    },
    {
      "$oid": "63fbe129b7d2392dbf426eac"
    },
    {
      "$oid": "63fbe0b5b7d2392dbf426e90"
    }
  ],
  "Parent": [
    {
      "$oid": "63fca551f8f86c84b0a0c0c1"
    }
  ],
  "createdAt": {
    "$date": {
      "$numberLong": "1677501025869"
    }
  },
  "updatedAt": {
    "$date": {
      "$numberLong": "1677501025869"
    }
  },
  "__v": 0
},{
  "_id": {
    "$oid": "63fceef2f8f86c84b0a0c0c4"
  },
  "CategoryName": [
    "Something for second one"
  ],
  "CategoryText": "<p>Hey this <strong>editor</strong> rocks </p>\n",
  "CategoryImage": [
    {
      "$oid": "63fbe20ab7d2392dbf426edc"
    },
    {
      "$oid": "63fbe129b7d2392dbf426eac"
    },
    {
      "$oid": "63fbe0b5b7d2392dbf426e90"
    }
  ],
  "Parent": [
    {
      "$oid": "63fca21a9576bde3872ab882"
    }
  ],
  "createdAt": {
    "$date": {
      "$numberLong": "1677501025869"
    }
  },
  "updatedAt": {
    "$date": {
      "$numberLong": "1677501025869"
    }
  },
  "__v": 0
}]

And here is the codeblock that I'm using right now:

let result = await category.aggregate([
        {
            $graphLookup: {
                from: 'categories',
                startWith: '$_id',
                connectFromField: '_id',
                connectToField: 'Parent',
                maxDepth: 4,
                depthField: "level",
                as: 'children'
            }
        },
        {
            $match: {
                Parent: null
            }
        }
    ]);

I'm really new to mongoose aggregate and this is giving me nightmares. Can anyone help me to move in correct direction please?

I'm really new to mongoose aggregate and this is giving me nightmares. Can anyone help me to move in correct direction please?

0 Answers0