Need to find all file shared within group and directly to user neo4j Cypher query

Hi, I need to find all the files directly shared with the user or via a group created by someone else.

I tried the following query it is giving results, however, if no file has been shared directly with the user or via group then I see three nodes with no relation

it might be possible the same file is shared directly to the user and with the group and the user is part of that group too. In this case, I need a file which is directly shared with the user not the group one.


I have also attached the dump for our database

match (u:user) where u.ohrid = "120"
match (f:file)
optional match (u)-[mg:MEMBER_OF_GROUP|CREATED_GROUP]->(g:group)<-[fswg:FILE_SHARED_WITH_GROUP]-(f)
optional match (f)-[fsw:FILE_SHARED_WITH]->(u)
return u, mg,fswg, g, fsw, f


There's probably a few ways to solve this, one approach could be to use the shortestPath function, which will collect files according to the relationship predicate you specify (docs @

match (u:user{ohrid:"127"}), (f:file), p = shortestPath((u)-[:FILE_SHARED_WITH|:FILE_SHARED_WITH_GROUP|:MEMBER_OF_GROUP*..2]-(f))
return u, p, f

Note I ran this for user "127", as "120" does not seem to have any files connected in your sample graph, and their only group "Reader" doesn't have any either.

Thanks, terry really appreciate, this query works. I have some permission data at a group level and file level. How can I check?
In relation to the user to a group (:MEMBER_OF_GROUP). We have some permission for the user which will be checked for the group and (:FILE_SHARED_WITH_GROUP) shared file with that group.
in relation FILE_SHARED_WITH we have some permission on file for that user.

if i get same file from both path then i will consider [:FILE_SHARE_WITH] relation as permssion for file else :MEMBER_OF_GROUP

Please let me know how can I achieve this.


Can you provide cypher statements to generate sample data? Will be easier to reason about.

Hi Terry,

Below is the whole sample database cypher queries. Looking forward to hearing from you.


I have a local copy of your graph setup but I'm still not sure I understand the problem you're trying to solve.

Can you try rephrasing it, perhaps as example questions to ask of the data you've provided?

Let's say user (Surabhi Misra). She is a [MEMBER_OF_GROUP] named (Tech) group. In this relation, we are storing group level permission for that user. There is a file (A8 Manual) which is shared with a (Tech) group node by another user named (Manish Kumar) node. (Surabhi Misra) is part of that group. She can access that file with group-level permission. She can only perform the task on that file based on that group's permission.

now someone else / same person shared the same file directly to that (Surabhi Misra). Then file level permission has higher precedence than the group level permission. We are creating a direct relation for the file to that user and keeping file level permission in a relationship (file)-(FILE_SHARED_WITH)->(user)

now I want to see all the file user has CREATED_FILE, FILE_SHARED_WITH, FILE_SHARED_WITH_GROUP by him/her, shared with him/her. if the same file has been shared via group and directly then I need the direct file, not the file shared with the group.
if no file created or shared by the user then the result should be empty.

So you need to confirm that the relationship path between a file and user (direct if available, otherwise via a group) contains a certain permission?

match (u:user{ohrid:8504}), (f:file), p = shortestPath((u)-[rels:FILE_SHARED_WITH|:FILE_SHARED_WITH_GROUP|:MEMBER_OF_GROUP*..2]-(f))
where any (rel in rels where 'read' in rel.documentPermission)
return u, p, f

Does this give you the result you're looking for?