cypher delete node and all the list of related nod

2019-05-29 21:19发布

问题:

I'm trying to delete an entire list starting from a given node to the end of the list.

Where is the list of the root,relationship and child nodes. The child nodes can have an undetermined nodes.

(r:Root {name:'masterDoc'})<-[p:previous]<-(s1:schema)<-[p1:previous]<-(s2:schema)<-[pn:previous]<-(sn:Schema)

When I run the cypher query below I'm getting a Type mismatch: expected Node, Path or Relationship but was Collection

MATCH (n:`Root` {name:'masterDoc'})-[r:previous*]-(s) delete s,r,n

Any Idea? 

回答1:

You want to pull out the longest path of nodes, iterate over the relationships and delete each one and then iterate over the nodes and delete them.

NOTE: This assumes that each node in the path is no longer anchored to anything other than the nodes in the path otherwise they will not be able to be removed.

// match the path that you want to delete
match p=(:Root {name: 'masterDoc'} )-[:previous*]->() 
with p
// order it in descending order by length
order by length(p) desc
// grab the longest one
limit 1
// delete all of the relationships
foreach (r in relationships(p) | delete r)
// delete all of the remaining nodes
foreach (n in nodes(p) | delete n)


标签: cypher