Retrieving folder structure from a query

Hi,

I'm writing a query but having a real hard time actually translating it effectively on the other end. I'm using neo4j to represent a folder structure - because it seems to be the perfectly logical way to do so. Example -

23

This way I can also attach users to appropriate folders and have figured out how to query if a user has access based on the folder chain, e.g.

OPTIONAL MATCH p = (:User { id: 1 })-[*]->(:Folder { id: 3 })
RETURN CASE WHEN p IS NULL THEN false ELSE true END as hasAccess

Additionally, I can work out the breadcrumb structure quickly and easily with something like

MATCH (:Folder { id: 5})<-[:CHILD*]-(f:Folder) RETURN f

However, what I ideally want to achieve is having a full directory structure so that in my code I can easily represent my folder structure as below

Folder 2
Folder 1
  | Folder 3
    | Folder 4
      | Folder 5
  | Folder 6

The problem is, to achieve this I'm sure I'd need the relationship, along with the node ID in each direction. Right now, this seems to be as far as I've got -

MATCH (p:Folder)-[r:CHILD]->(c:Folder) RETURN p as folder, collect(c) as children

Would appreciate any help as I slowly wrap my head around this awesome query language!

You could use reduce or filter/map on relationships(path) or nodes(path)

APOC has a call apoc.convert.toTree procedure that might help you here. Where you just pass in your paths.

apoc.convert.toTree([paths],[lowerCaseRels=true], [config]) creates a stream of nested documents representing the at least one root of these paths