How can i fetch hierarchical data with their refrences in mongodb?

I have a MongoDB collection Department as documents with the following format:

        {
            "department_id" : 1, 
            "title" : "Department1",
            "departmentType_id" : 11
        }
        {
            "department_id" : 2, 
            "title" : "Department2",
            "departmentType_id" : 12
        }
        {
            "department_id" : 3, 
            "title" : "Department3",
            "departmentType_id" : 13
        }
        {
                "department_id" : 4, 
                "title" : "Department4",
                "departmentType_id" : 14
        }
        {
                "department_id" : 5, 
                "title" : "Department5",
                "departmentType_id" : 15
        }
        {
                "department_id" : 6, 
                "title" : "Department6",
                "departmentType_id" : 16
        }
        {
                "department_id" : 7, 
                "title" : "Department7",
                "departmentType_id" : 17
        }

I have a MongoDB collection departmentDepartmentRelationship as documents with the following format:

{
    "departmentDepartmentRelationshipId" : 100,
    "relationshipTitle" : "Department1 is parent of Department2",
    "department1_id" : 1,//reference of department with department_id: 1
    "department2_id" : 2,//reference of department with department_id: 2
    "rootParentDepartment_id" :1,//reference of department with department_id: 1
    "rootParentDepartment_id" : 1,//reference of department with department_id: 1
}
{
    "departmentDepartmentRelationshipId" : 200,
    "relationshipTitle" : "Department2 is parent of Department3",
    "department1_id" : 2,//reference of department with department_id: 2
    "department2_id" : 3,//reference of department with department_id: 3
    "rootParentDepartment_id" :1,//reference of department with department_id: 1
    "rootParentDepartment_id" : 1,//reference of department with department_id: 1
}
{
    "departmentDepartmentRelationshipId" : 210,
    "relationshipTitle" : "Department2 is parent of Department7",
    "department1_id" : 2,//reference of department with department_id: 2
    "department2_id" : 7,//reference of department with department_id: 7
    "rootParentDepartment_id" :1,//reference of department with department_id: 1
    "rootParentDepartment_id" : 1,//reference of department with department_id: 1
}
{
    "departmentDepartmentRelationshipId" : 300,
    "relationshipTitle" : "Department3 is parent of Department4",
    "department1_id" : 3,//reference of department with department_id: 3
    "department2_id" : 4,//reference of department with department_id: 4
    "rootParentDepartment_id" :1,//reference of department with department_id: 1
    "rootParentDepartment_id" : 1,//reference of department with department_id: 1
}
{
    "departmentDepartmentRelationshipId" : 400,
    "relationshipTitle" : "Department4 is parent of Department5",
    "department1_id" : 4,//reference of department with department_id: 4
    "department2_id" : 5,//reference of department with department_id: 5
    "rootParentDepartment_id" :1,//reference of department with department_id: 1
    "rootParentDepartment_id" : 1,//reference of department with department_id: 1
}
{
    "departmentDepartmentRelationshipId" : 500,
    "relationshipTitle" : "Department4 is parent of Department6",
    "department1_id" : 4,//reference of department with department_id: 4
    "department2_id" : 6,//reference of department with department_id: 6
    "rootParentDepartment_id" :1,//reference of department with department_id: 1
    "rootParentDepartment_id" : 1,//reference of department with department_id: 1
}

Now my model where i define hierarchical structure is:

{
    "_id" : ObjectId("60af33d948d800a1315e96f6"),
    "departmentDepartmentRelationshipId" :100,
    "childs" : [ 
        {
            "departmentDepartmentRelationshipId" : 200,
            "childs" : [ //child could by more than one
                {
                    "departmentDepartmentRelationshipId" : 300,
                    "childs" : [//child could by more than one
                        {
                          "departmentDepartmentRelationshipId" : 400,
                               "childs" : []
                        }
                        {
                          "departmentDepartmentRelationshipId" : 500,
                               "childs" : []
                        }

                   ]
                },
               {
                      "departmentDepartmentRelationshipId" : 210,
                        "childs" : []
               }
            ]
        }
    ]
}

is it possible to fetch above hierarchical model in a single query and instead of its references i want departmentDepartmentRelationship JSON object from departmentDepartmentRelationship collection ?
iā€™m an absolute beginner in mongoDB , i would be very thankful if anyone can help me.

Hi @MANZAR_ABBAS,

What you are doing looks a bit SQLish to me.

Have a look to this doc:

Maybe something will help?

Cheers,
Maxime.

2 Likes