Joins
NoQL supports INNER
and OUTER
Joins, but does not unwind by default and the items are added as an array field.
Join Hints
There are several join hints to simplify document model joining:
- first
- last
- unwind
- optimize
Join hints are added using a pipe |
character to the join table or alias.
Example join hint
first
Returns the first element of the join result as an object on the result
Example first
join hint
last
Returns the last element of the join result as an object on the result
Example last
join hint
unwind
Unwinds the result into multiple records following the result of the join
Example unwind
join hint
optimize
The optimize hint is a current workaround to limit the result set for the $lookup when working with sub selects.
The $match on sub query joins is applied after the subquery pipeline which can cause performance issues since indexes may not be used. It may be better to put the match before the pipeline to limit the input set depending on the on conditions.
Example optimize
usage
The on field must be part of the sub query select to be a valid optimization.
JOIN
Array Functions
Alternatively the explicit array functions FIRST_IN_ARRAY
, LAST_IN_ARRAY
, UNWIND
can be used instead of the join hints:
Example FIRST_IN_ARRAY
usage
Example LAST_IN_ARRAY
usage
Example UNWIND
usage
Caveats
An IN
sub-select on a WHERE
clause does not work as a join. Use a join instead.