I'm not good at documents DB, wanna improve myself. The task is: to store an enterprise hierarchy into Mongo DB.
There are employers and departments. Every employer belongs to a particular department ( BTW what if someone belongs to multiple departments?). Sure, there is employees hierarchy: Director is a root. So everyone except director has a direct boss ( what if someone has multiple bosses which are not from the same branch?)
The structure could be changed ( lets say not more often than once per a day) and has a timestamp.
So how to store it into Mongo DB? Multiple collection? Or one collection and many documents?
All viable ideas are appreciated.
Store each employee as a single document. In that document store an array of departments and an array of direct reports. ICs have a direct reports array that is empty. Index the reports array so queries are efficient.
This structure supports multiple departments per employee and multiple managers per employee.
Most hierarchies change atomically so I would suggest changes be applied to a new collection and that collection replace the old collection.
You can use the MongoDB $graphLookup operator to query the graph of employees.