如何使用Cypher在两个节点之间找到中间节点

时间:2018-05-07 12:35:40

标签: neo4j cypher spring-data-neo4j

Department with Users我们有部门树,如图所示

我需要列出节点D1和D1.A1.B1

之间节点的所有部门节点和关联用户

我需要一个Cypher查询。

结果应该是部门: D1,D1.A1,D1.A1.B1 和用户:U1,U2,U4

1 个答案:

答案 0 :(得分:1)

使用variable-length pattern matching,nodes(),unwindcollect()的以下Cypher查询应该有效。评论说明:

// match the entire path from 'D1' to 'D1.A1.B1'
match p = ( {name : 'D1'} )-[*]->( {name : 'D1.A1.B1'} )
// get all nodes (departments) from path
with nodes(p) as deps
// unwind deps collection to individual departments
unwind deps as dep
// match workers and managers directly connected to dep nodes
match (dep)<-[:WORKER|:MANAGER]-(u:User)
return collect(dep) as departments, collect(u) as users