0

I'm trying to make a select query with 2 inner joins over 3 tables to be able to show all tasks for the whole company.

Meaning I have three tables and I would like to obtain the tasks of all the Employees within the company.

Table 1: Companies

===================
ID     CompanyName
===================
1      Company1
2      Company2

Table 2: Employes

=============================
ID     Name         CompanyID
=============================
1      Employee1    1
2      Employee2    1
3      Employee3    2

Table 3: Tasks

==============================
ID     TaskTitle    EmployeeID
==============================
1      Feature1     1
2      Feature2     2
3      Feature3     3
4      Feature4     1

So result for companyID === 1 should be tasks 1, 2 and 4.

I know you can populate data using mongoose but only if there is a relationship in the table where you are trying to populate those data. Which is not in this approach of relationships using only parent ids.

Is there a way to do this in mongo using mongoose (keystoneJS)?

Any help is much appreciated! Many thanks...

Privorka
  • 21
  • 5
  • I think it's what are you looking for https://stackoverflow.com/questions/19222520/populate-nested-array-in-mongoose – ByteMaster Jan 16 '18 at 21:38
  • @ByteMaster this approach doesn't work, because I just know the company I want the data for. That means I'm coming from the other side. I want data for company 1. But company doesn't know anything about it's children. Only children know who is their parent. The `populate` function would work from the other side. If I came from children. Meaning if I would want to know data about the tasks employee [task (children) knows who is it's employee (parent)], then it would work. Meaning that you can `populate` only the relation that is returned. – Privorka Apr 01 '18 at 22:47

0 Answers0