【发布时间】:2016-07-25 22:03:05
【问题描述】:
你能帮我建立密码查询吗?我有以下图形数据库结构:
(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 可以进行递归调用吗?我觉得这样会更好。
谢谢。
附:我知道关于 SO 有类似的问题,但它们对我没有帮助。
【问题讨论】:
-
是的,Cypher 并没有真正进行递归。返回数据树时,最好的选择是使用以下cybersam 建议的内容,或者将节点/关系作为表格返回并在内存中构建它们。