N1QL Concatenate many children rows into single string

可紊 提交于 2019-12-11 11:45:48

问题


Consider in Couchbase I have a person database, with 3 documents:

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

Person B

Person C
    Child CA
    Child CB
        Grand CBA

Is there an easy way to turn this into following results using N1QL? I am trying to display each person with their grand children displayed into strings?

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

回答1:


You can do the following:

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;


来源:https://stackoverflow.com/questions/35742657/n1ql-concatenate-many-children-rows-into-single-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!