在neo4j中以嵌套的json形式表示无限深度节点

时间:2016-05-19 06:50:38

标签: json neo4j cypher graph-databases spring-data-neo4j

我有一个图表,每个节点与其子节点具有父关系(如附图所示)和一个名称属性。 enter image description here我想在嵌套的json对象中显示这个图,如下所示:

{name: A,
 children: [{name: A1,
           children: A11, A12, A13 },

            {name: A2,
            children: A21, A22, A23},

            {name: A3,
             children: A31, A32, A33}
           ]}

我想遍历任何深度,所以关系应该是(a) - [*] - >(b)。 参考this question,我想出了以下查询:

match(parent{name: "A"})-[*]->(child)
with parent, child, collect(child.name) as children
return {name: parent.name,
    children: collect({name: child.name,
                      children: children})}

但上述查询会产生以下结果:

    name    A
    children    [
    name    A23
    children    [A23]
    ,
    name    A22
    children    [A22]
    ,
    name    A32
    children    [A32]
    ,
    name    A2
    children    [A2]
    ,
    name    A12
    children    [A12]
    ,
    name    A21
    children    [A21]
    ,
    name    A31
    children    [A31]
    ,
    name    A1
    children    [A1]
    ,
    name    A11
    children    [A11]
    ,
    name    A33
    children    [A33]
    ,
    name    A3
    children    [A3]
    ,
    name    A13
    children    [A13]
    ] 

那么,如上所述,以json对象的形式表示这个完整树的可能解决方案是什么?

1 个答案:

答案 0 :(得分:1)

我向APOC添加了这样的功能,请随时查看: https://github.com/neo4j-contrib/neo4j-apoc-procedures#fromtojson

FBSDKAccessToken.currentAccessToken()