1 min readApr 25, 2019
Getting the next/previous item from a sorted list
Continuing on from the queries I am currently working on, I am creating a new item into the graph, and as a result, I need to update previous and next item(s) based on date. Think of some type of versioning, if you will…
My node may look like this:
(:Node {fromDate:'2015-01-12', toDate:'2099-12-31})
If I add a new node that’s got a fromDate after 2015–01–12, I need to update the toDate on this node to the previous day. I also will have many nodes that meet the condition of before 2015–01–12, but I only what the latest one. We can do this with the following (assuming the new node as a date the following day):
MATCH (nPrev:Node) WHERE nPrev.fromDate < '2015-01-13'
WITH nPrev ORDER BY nPrev.fromDate DESC LIMIT 1
SET nPrev.toDate = nNew.fromDate -1
<rest of your query>