cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to splice a Linked List in NEO4j's cypher

I asked the question in SO: https://stackoverflow.com/questions/60765877/how-to-splice-a-linked-lists-in-cypher/60771324#6077132...

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

0 REPLIES 0