1

Currently i have one collection "Category" which has a self join with ref on parent column. In that I have 3 Document.

  • Document 1 is the parent.
  • Document 2 is the child of Document 1
  • Document 3 is the child of Document 2

CategoryModel

var CategoryTable = new Schema({
categoryName : String,
parent : {
    type : ObjectID,
    ref : 'category',
    default : null
}
});

MyCode

CategoryModel
    .aggregate([
        {
            $match : { "parent" : null }
        },
        {
            "$lookup":{
                "from" : "categories",
                "localField":"_id",
                "foreignField":"parent",
                "as": "child"
            }
        }
    ])
    .exec((err,data) => {
        if(err)
        {
            throw err;
        }
        res.send(data);
    })

Current Output

[
{
    "_id": "5d8de924b4672e2744dedbb9",
    "parent": null,
    "categoryName": "Software",
    "__v": 0,
    "child": [
        {
            "_id": "5d8de972b4672e2744dedbba",
            "parent": "5d8de924b4672e2744dedbb9",
            "categoryName": "Antivirus",
            "__v": 0
        }
    ]
}
]

Expected Output

[
{
    "_id": "5d8de924b4672e2744dedbb9",
    "parent": null,
    "categoryName": "Software",
    "__v": 0,
    "child": [
        {
            "_id": "5d8de972b4672e2744dedbba",
            "parent": "5d8de924b4672e2744dedbb9",
            "categoryName": "Antivirus",
            "__v": 0,
            "child": [
                         {
                           "_id": "5d8e1a303bcfb6085c48e4dc",
                           "parent": "5d8de972b4672e2744dedbba",
                           "categoryName": "Quick Heal",
                           "__v": 0
                         }
                    ]
        }
    ]
}
]

I want to fetch all the child and subchild collection using aggregate. Can someone please help me in this?

  • 1
    Why not use populate? – Cuong Le Ngoc Oct 07 '19 at 10:04
  • 1
    So there is unknown depth for the parent & child keys? If yes then there is no way this can be achieved using aggregation. `$graphLookup` can be the one choice but it also available for one deep level. May be this answer help you [Hierarchical queries with Mongo using $graphLookup](https://stackoverflow.com/questions/52433933/hierarchical-queries-with-mongo-using-graphlookup) – Ashh Oct 07 '19 at 10:07
  • @CuongLeNgoc I have tried populate but it doesn't give the result as expected. – Shashikant Maurya Oct 07 '19 at 10:20
  • 1
    @Ashh $graphLookup not giving the expected result but is able to fetch all the child and subchilds. – Shashikant Maurya Oct 07 '19 at 10:45
  • That is what I said in the above comment. It will only work for one deep level not for the nested ones and that's why I have provided you a link which might can help you but fairly you must go with `.mapReduce` here. – Ashh Oct 07 '19 at 10:59
  • Possible duplicate of [Hierarchical queries with Mongo using $graphLookup](https://stackoverflow.com/questions/52433933/hierarchical-queries-with-mongo-using-graphlookup) – Alex Blex Oct 07 '19 at 14:19

0 Answers0