1
你能帮我建立密码查询吗?我有以下图形数据库结构:neo4j Cypher分层树构建响应JSON
(parent:Category)-[:subcategory]->(child:Category)
有了这个图数据我有深层次的树状层次结构。
我发现下面的代码上Stackoverfllow.com,改变了我的数据:
MATCH (root:Category)-[:subcategory]->(parent:Category)-[:subcategory]->(child:Category)
WITH root, {category: parent, children: collect(child)} AS parent_with_children
WHERE NOT(()-[:subcategory]->(root))
RETURN {category: root, children: collect(parent_with_children)}
但他只建了3个层次树的深度反应。我需要更大。我试图建立像这个例子的JSON响应:
[
category: {
name: "PC"
children: {
category: {
name: "Parts"
children: {
category: {
name: "CPU"
...
}
}
},
category: {
name: "Accessories"
...
}
}
},
category: {
name: "Laptop"
...
}
]
Cypher可以进行递归调用?我认为这会更好。
谢谢。
P.S.我知道有类似的问题,但他们没有帮助我。
是的,Cypher并没有真的做递归。当你返回一棵数据树时,你最好的选择是使用cybersam在下面建议的内容,或者将节点/关系作为表格返回,并将它们构建在内存中。 –