如何在N1QL中进行正确的LEFT OUTER JOIN

时间:2016-09-02 09:08:00

标签: couchbase n1ql

我已经创建了一个索引,以便在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;

当我收到该特定用户的订单时,此功能正常。问题是当我没有任何订单时,我根本就没有结果。

我注意到如果从查询中删除AND o.docType="Order"语句,我会得到一些结果,但是如果有文档也有userId属性会怎么样?我怎样才能让它正常工作?

1 个答案:

答案 0 :(得分:1)

@a_horse_with_no_name基本上是正确的。使用IS MISSING而不是IS NULL:

AND (o.docType="Order" OR o.docType IS MISSING)