N1QL (Couchbase) - 在 couchbase 中为不存在的关系提供真正的 LEFT JOIN

N1QL (Couchbase) - True LEFT JOIN in couchbase for non-existing relationships

为简单起见,假设我有以下数据库:

{
    id: "a_id",   // This is the key, not a field in the document
    type: "a"
}
{
    id: "b_id",
    type: "b",
    a: "a_id"
}
{
     type: "c",
     b: "b_id"
}

如你所见,有一个经典的父子关系("a" HAS MANY "b", "b" HAS MANY "c") 假设不会总是有 "c" 类型的文档 - 例如,我有五个 "b" 文档,但只有一个有一个 "c" 文档与之相关。 我想写一个 JOIN 查询,它将给我属于 "a" 的所有 "b",如果它们存在 - 所有属于 "b" 的 "c",但是对于 "b" 没有 "c" 的文件我没有得到任何结果 - 我想获得所有五个 "b" 文件,以及一个 "c" 相关的文件. 我的查询如下所示:

SELECT *
FROM default AS a
JOIN default AS b ON KEY b.a FOR a
JOIN default AS c ON KEY c.b FOR b
WHERE a.type = "a"
AND b.type = "b"
AND c.type = "c";

我在这里错过了什么? 谢谢!

WHERE 子句 post join filter if if c is MISSING c.type = “c” is false.

INSERT INTO default VALUES("a_id", { "type": "a" });
INSERT INTO default VALUES("b_id", { "type": "b", "a":"a_id"});
INSERT INTO default VALUES("b_id1", { "type": "b", "a":"a_id"});
INSERT INTO default VALUES("b_id1", { "type": "b", "a":"a_id"});
INSERT INTO default VALUES("b_id2", { "type": "b", "a":"a_id"});
INSERT INTO default VALUES("b_id3", { "type": "b", "a":"a_id"});
INSERT INTO default VALUES("b_id4", { "type": "b", "a":"a_id"});
INSERT INTO default VALUES("c_id", { "type": "c", "b":"b_id"});

CREATE INDEX ia ON default(a);
CREATE INDEX ib ON default(b);

SELECT *
FROM default AS d
LEFT JOIN default AS d1 ON KEY d1.a FOR d
LEFT JOIN default AS d2 ON KEY d2.b FOR d1
WHERE d.type = "a"
AND d1.type = "b"
AND (d2 IS MISSING OR d2.type = "c");

也结帐https://dzone.com/articles/visually-explaining-n1ql-joins