Is there a MongoDB aggregation query equivalent to the following SQL statement using a single MongoDB aggregation? (The two operands of the JOIN are two subqueries that also include join operations.)
SELECT
T1.C1, T2.C2
FROM
(SELECT
T3.C1 C1, T4.C2 C2
FROM
T3
JOIN
T4 ON T3.C0 = T4.C0) T1
JOIN
(SELECT
T5.C1 C1, T6.C2 C2
FROM
T5
JOIN
T6 ON T5.C0 = T6.C0) T2 ON T1.C1 = T2.C1
AND T1.C2 = T2.C2;
2
Answers
Short answer – no, lookup requires "from" to be a collection.
As WD pointed out, SQL patterns do not work in NoSQL universe, yet database is a database – if data is there you can always fetch it. The logic will be inverted – you will do
SELECT T3.C1, T6.C2 FROM T3 and complex lookups/unwinds from T4, T5 and T6
.Considering the OP’s snippet, it would be fair to assume the question is more academic than practical. From this perspective – it’s still "no". Don’t design/denormalise data to the degree that you find yourself in the position where you need to run such query.
This does not exactly address the OP question re. subqueries but show how, if absolutely necessary, "temporary" collections can be used in lieu of subqueries. Note that in the original SQL, only tables
T3
.T4
,T5
, andT6
actually exist;T1
andT2
are subquery constructs. Also, the original SQL is not particularly useful because it only shows matching columns; we know nothing else about the rows from whence they came, so we will carry alongold_id
just for show.yields