Cypher match any node connected to a node filtered by last relation

Viewed 20

I have a neo4j graph looking like this:

(AdminUser) - [is_admin] -> (Tenat) - [can_edit,can_read] -> (Resource)

(RegularUser) - [can_edit] -> (Resource)

(RegularUser2) - [can_read] -> (Resource)

This a simplified version, so I want to get all Resource that a given user is connected on any path, but such that last relationship before Resource node is 'can_edit'

I would try something like

match(u:User{id:'regular'}) // I'd like to insert user id here

-[*]->(r:Resource)  // here not sure how to limit last relation as can_edit

In the example above, I'd like to get Resource when if I insert either AdminUser or RegularUser id, but not RegularUser2

1 Answers

You can do something simple like:

match(u:User{id:'regular'})-[*0..]->()-[:can_edit]->(r:Resource)  

This will make sure the the last relationship before the Resource is [:can_edit]

Related