我正在匹配一个完整的路径,删除最后一个节点,然后沿着节点返回以删除任何没有向下关系的节点。
这个查询是我想要的:
MATCH (t:type)-[:HAS]->(m:model)-[:HAS]-(r:region)-[:HAS]-(p:param)-[:HAS]-(i:init { value: 1613044800})-[init_has:HAS]-(u:{url: 'http://something2.png'})
DELETE init_has, u
WITH i,p,r,m,t
MATCH (i) WHERE NOT (i)-[:HAS]->()
DETACH DELETE i
WITH p,r,m,t
MATCH (p) WHERE NOT (p)-[:HAS]->()
DETACH DELETE p
WITH r,m,t
MATCH (r) WHERE NOT (r)-[:HAS]->()
DETACH DELETE r
WITH m,t
MATCH (m) WHERE NOT (m)-[:HAS]->()
DETACH DELETE m
WITH t
MATCH (t) WHERE NOT (t)-[:HAS]->()
DETACH DELETE t
但是这个查询只删除WITH语句中的第一个节点,i
:
MATCH (t:type)-[:HAS]->(m:model)-[:HAS]-(r:region)-[:HAS]-(p:param)-[:HAS]-(i:init { value: 1613044800})-[init_has:HAS]-(u:{url: 'http://something2.png'})
DELETE init_has, u
WITH [i,p,r,m,t] AS parents
UNWIND parents AS parent
WITH parent
MATCH (parent) WHERE NOT (parent)-[:HAS]->()
DETACH DELETE parent
离开节点p
,该节点没有任何向下的关系。为什么这些查询不同?我是否可以修改第二个查询以正确地遍历这些节点并删除它们,如果它们没有向下的关系?
我不太确定是什么问题。我可以给你一些提示,告诉你如何改进你的查询。当您希望在后面的步骤中执行过滤时,您不必使用另一个MATCH语句。在你的例子中:
MATCH (t:type)-[:HAS]->(m:model)-[:HAS]-(r:region)-[:HAS]-(p:param)-[:HAS]-(i:init { value: 1613044800})-[init_has:HAS]-(u:{url: 'http://something2.png'})
DELETE init_has, u
WITH [i,p,r,m,t] AS parents
UNWIND parents AS parent
WITH parent
WHERE NOT (parent)-[:HAS]->()
DETACH DELETE parent
我不确定这是否能解决你的问题。查询看起来很好,所以没有例子很难说出哪里出错了。