N1QL将许多子行连接成单个字符串

时间:2016-03-02 09:06:02

标签: couchbase n1ql

在Couchbase中考虑我有一个人员数据库,有3个文件:

Person A
    Child AA
        Grand AAA
        Grand AAB
    Child AB
        Grand ABA
        Grand ABB

Person B

Person C
    Child CA
    Child CB
        Grand CBA

有没有一种简单的方法可以使用N1QL将其转换为以下结果? 我试图将每个人的大孩子展示成字符串?

Person A, Grand: "AAA, AAB, ABA, ABB"
Person B, Grand: ""
Person C, Grand: "CBA"

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

SELECT p.name AS p, ENCODE_JSON(ARRAY_AGG(g.name)) AS g
FROM person AS p LEFT OUTER UNNEST p.child AS c LEFT OUTER UNNEST c.grand AS g
GROUP BY p;