Say you want to simulate a specific situation. A company may have one or more branches. And these branches have employees who can work in different companies (or even in two different branches of the same company). This, of course, is just an example.
Suppose also that most inquiries / inquiries will be performed in collections of employees and companies.
The first (naive) way to do this is to implement everything (the Company has an array of branches and branches with many employees):
{ name: "Company name", // other company data branches : [ { name: "Branch name", // other branch data Employees: [ { // employee1 data }, { // employee data }, ] } ] }
But this would be very ineffective if someone was interested in obtaining information about employees (you would need to get a company, and then iterate through each branch to find the employee that is required).
On the other hand, you can use links and simulate RDBMS (there will be a collection of the company, branch and employee), but this will mean more requests.
The third option (to which I am closest) will have Employee as a separate collection, and then have an array of links to it in the branches. In addition, to provide faster requests, such as “employees with specific names that work for a specific company and a specific industry,” ObjectId can be stored in the Employee collection:
{ company_id: "some id", first_name: "First name", last_name: "Last name", // }
So, in this case, to search for all employees with certain names that work for a certain company and a certain branch, it would be necessary to fulfill two queries. The first query will return companies that satisfy the "company condition" (company name and branch name), and then the second query in the Employee collection will return all employees who have given a name and who work in companies whose identifiers are returned in the first query.
Would you do it differently? Is there any other “recommended” way to do this? Could you add some improvements?
More importantly, what to do in a situation where these two queries return multiple results that have a small intersection? How to increase productivity in this case?