Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

neo4j - how to insert a node in a middle of a linked list in cypher

I am using linked list pattern in cypher to represent some data.

(node1)-[:NEXT]->(node2)-[:NEXT]->(node3)

the question is how can I insert newNode between the linked list nodes. so that I get

(node1)-[:NEXT]->(newNode)-[:NEXT]->(node2)-[:NEXT]->(node3) for example.

there are some corner cases where the newNode can be at the end of the list or the beginning and the solution needs to account for them.

is there an elegant end easy way to do it without multiple apoc.do.when calls?

question from:https://stackoverflow.com/questions/65865919/how-to-insert-a-node-in-a-middle-of-a-linked-list-in-cypher

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

All you need to do is plumb in the new node, and then remove the old relationships, something like... (I'm assuming some properties here)

MATCH (n1:Node {name:'node1'})-[r:NEXT]->(n2)
CREATE (n1)-[:NEXT]->(:Node {name: 'new Node'})-[:NEXT]->(n2)
DELETE r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...