I have created an index in order to perform the following left outer join in N1ql:
CREATE INDEX idx_test ON bucket("User::" || userId) WHERE docType="Order";
SELECT u.*, ARRAY_AGG(o) as orders
FROM bucket AS u
LEFT OUTER JOIN bucket AS o ON KEY "User::" || o.userId FOR u
WHERE u.docType="User"
AND o.docType="Order"
GROUP BY u;
This works fine when I have orders for that particular user. The problem is when I don't have any orders, then I get no results at all.
I have noticed that if I remove the AND o.docType="Order" statement from the query I get some results, but what happens if there are documents that also have a userId property? How can I get this to work correctly?
AND o.docType="Order"to the join condition (out of the where). I don't know n1ql so I don't know how you specify more then one condition in the join clause there. - a_horse_with_no_nameAND (o.docType="Order" or o.docType IS NULL)- a_horse_with_no_namedocTypeis missing, then the key used in theLEFT OUTER JOINwill be null, yielding an empty result set. - Manuel Reis