Я хочу получить иерархические данные в MongoDB с их ссылками?

#database #mongodb #mongodb-query #aggregation-framework #nosql-aggregation

Вопрос:

У меня есть отдел сбора MongoDB в виде документов следующего формата:

 {
    "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
}
 

У меня есть связь с отделом сбора MongoDB в виде документов следующего формата:

 {
    "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" : []
               }
            ]
        }
    ]
}
 

можно ли получить вышеуказанную иерархическую модель в одном запросе, и вместо ее ссылок мне нужен объект JSON departmentDepartmentRelationship из коллекции departmentDepartmentRelationship ?
Мой ожидаемый результат таков::

 {
        "_id" : ObjectId("60af33d948d800a1315e96f6"),
        {
        "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
    },
        "childs" : [ 
            {
                {
        "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
    },
                "childs" : [ //child could by more than one
                    {
                        {
        "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
    },
                        "childs" : [//child could by more than one
                            {
                               {
        "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
    },
                                   "childs" : []
                            }
                            {
                              {
        "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
    },
                                   "childs" : []
                            }

                       ]
                    },
                   {
                           {
        "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
    },
                            "childs" : []
                   }
                ]
            }
        ]
    }
 

я абсолютный новичок в MongoDB , я был бы очень благодарен, если бы кто-нибудь мог мне помочь.

Комментарии:

1. Если вы используете Мангуста для управления этой базой данных (это означает, что ваш проект написан в узле), есть довольно простой способ сделать то, что вы хотите, который заключается в использовании метода .populate (). Это было бы ваше дело?

2. но как я могу пересечь свою модель?