1
votes

Department with UsersWe have tree of Departments as shown in the picture

I need to list all department nodes and associated users for node between node D1 and D1.A1.B1

I need a Cypher query for this.

Result should be department: D1, D1.A1, D1.A1.B1 and users: U1,U2,U4

1

1 Answers

1
votes

The following Cypher query using a variable-length pattern matching, nodes(), unwind and collect() should work. Explanation on comments:

// 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