我从这个答案中得到了帮助它运行正常,但对于孤立节点而言并不意味着没有关系的单个节点(子节点)。 在这种情况下我得到的甚至不是那个节点。请帮助我,我是neo4j的初学者,这将是非常有利的
OPTIONAL MATCH path = (x)-[*0..100]->()
WHERE ID(x) = 65
UNWIND nodes(path) as node
UNWIND rels(path) as rel
WITH collect(distinct node) as nodes,collect(distinct rel) as rels
// WITH apoc.coll.flatten(collect(nodes(path))) as nodes, apoc.coll.flatten(collect(relationships(path))) as rels
WITH apoc.coll.toSet([n in nodes WHERE n is not null
| { id: id(n),label: labels(n),type:"",metadata: properties(n) } ]) as nodes,
apoc.coll.toSet([r in rels WHERE r is not null
| { id: id(r),source: id(startNode(r)),relation: type(r),target: id(endNode(r)), directed: "true" } ]) as rels
RETURN { graph: { type:"",label: "",directed: "true",nodes: nodes,edges: rels,
metadata:{ countNodes: size(nodes),countEdges: size(rels) } } } as graph;
由于
答案 0 :(得分:1)
问题是:UNWIND rels(path) as rel
UNWINDing集合意味着获取该记录,并将其更改为集合中每个元素的记录,它会使记录倍增。当集合为空(孤立节点没有关系)时,它的乘数为零......它会清空带有空集合的记录。
您可以使用CASE语句将集合替换为null而不是空集合(当您再次收集时,null将被清除)。当你UNWIND时,它会保留记录。
UNWIND case when size(rels(path)) = 0 then [null] else rels(path) end as rel