I asked the question in SO: neo4j - How to Splice a Linked Lists in Cypher - Stack Overflow
Given a linked-list of LogEntry
:
(log:Log)
(e1:LogEntry {date: Date('2020-03-19')})
(e2:LogEntry {date: Date('2020-03-17')})
…
CREATE (log)-[:PREV_ENTRY]->(e1)
CREATE (e1)-[:PREV_ENTRY]->(e2)
CREATE (e2)-[:PREV_ENTRY]->(e3)
CREATE (e3)-[:PREV_ENTRY]->(e4)
How to I splice in a new Log Entry of random date?
I feel like I'm 70% there with:
MATCH p=(log:Log {id: "log_abc"})-[:PREV_ENTRY*]->(e:LogEntry)
FOREACH (rel IN relationships(p) |
DELETE rel
)
WITH e, log
ORDER BY e.id DESC
WITH collect(e) AS entries, log, e
CALL apoc.nodes.link(entries, 'PREV_ENTRY')
WITH apoc.agg.first(e) AS latest, log
CREATE (log)-[:PREV_ENTRY]->(latest)
RETURN latest
Any help here would be awesome! That way I can answer my own SO question too. Thanks