B
B
Bruto2021-09-30 18:15:54
NoSQL
Bruto, 2021-09-30 18:15:54

How to build a hierarchy in MongoDB?

There is a table. There are employees in it, and each employee has an ID, and there is an ID of the manager.
I need to build a hierarchy, or group them in such a way that I understand whether a particular subordinate is a subordinate of the leader in the hierarchy.
Conditionally:

[
{emp: 1, leader: 2},
{emp:2, leader:5},
{emp:5, leader: 10}
];

Here I need to understand that the employee under emp 1 is a subordinate of the employee emp 5 (hierarchically, based on the leader )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bruto, 2021-09-30
@BruTO8000

The pipeline operator $graphLookup helped :

{
      $graphLookup: {
        from: "employees",
        startWith:
          "$emp",
        connectFromField: "leader",
        connectToField: "emp",
        as: "leadersOfCurrentEmployee",
      },
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question