0
votes

I'm pretty new to neo4j and I managed to create nodes and relationships between them. But now I'm trying to get the last nodes. Given I've the following graph:

enter image description here

Each node references the previous day. Now I want start at "Day 1" to get the last node ("Day 4") to determine it's date. How can I do this? After I read https://neo4j.com/docs/cypher-manual/current/functions/scalar/#functions-endnode I tried MATCH mypath = (b:Days)<-[prevDay]-(endnode) RETURN endNode(prevDay) AS lastDay. Unfortunately this query returns me Day 1, Day 2 and Day 3. The last day ("Day 4") is not returned. So how can I get "Day 4" only?

2

2 Answers

2
votes

I would do something like this:

MATCH path=(:Days {name:'Day 1'})<-[:prevDay*]-(lastDay:Days)
WHERE NOT (lastDay)<-[:prevDay]-()
RETURN lastDay
0
votes

You can use variable-length relationship patterns such as:

MATCH path=(:Days)<-[:prevDay*]-(lastDay:Days)
WITH lastDay
ORDER BY LENGTH(path) DESC
LIMIT 1
RETURN lastDay