Francesco Gallarotti
Francesco Gallarotti

Reputation: 1227

How to get all nodes in a neo4j linked list?

In neo4j I have the following structure (essentially a linked list of linked lists):

(projects)
     |
[LatestProject]
     |
     V
(p1:Project)-[LatestTask]->(t1:Task)-[PrevTask]->(t2:Task)-[PrevTask]->(t3:Task)
     |
[PrevProject]
     |
     V
(p2:Project)-[LatestTask]->(t4:Task)-[PrevTask]->(t5:Task)-[PrevTask]->(t6:Task)
     |
[PrevProject]
     |
     V
(p3:Project)-[LatestTask]->(t7:Task)-[PrevTask]->(t8:Task)

I'd like to write a Cypher query that, given a project ID, returns all Tasks (from the latest one to the oldest one).

I tried the following:

start p=node(2)  // project ID = 2 
MATCH p-[:LatestTask]->(first:Task)-[:PreviousTask*]->(t:Task) 
return first, t

but this returns a table of several rows:

(first) (t1)
(first) (t2)

How can I return the following, instead?

(first)
(t1)
(t2) 

Upvotes: 0

Views: 330

Answers (1)

Francesco Gallarotti
Francesco Gallarotti

Reputation: 1227

I think I found the answer to my own question:

start p=node(2)  // project ID = 2 
MATCH p-[:LatestTask|PreviousTask*]->(t:Task) 
return t

seems to be return the correct set as three separate rows.

Upvotes: 1

Related Questions