问题描述
这是一个简单的图形:
(:a)-[:r]->(:b)
如果要删除(:b)
,我可以使用以下方法进行操作:
If want to to delete (:b)
, I can do this with:
MATCH (a)-[r]->(b :b)
DELETE a, r, b
但是,(b)
可以具有多个关系和脱离的节点(这些节点也可以递归地具有更多的关系和节点).像这样:
However, (b)
can have multiple relationships and nodes coming off of it (and those nodes can recursively have more relationships and nodes too). Something like this:
(:a)-[:r]->(:b)-[:s]->(x)-[:r]->(y)- ... ->(z)
如何递归删除除(b)
之外的每个节点和关系?
How can I recursively delete every node and relationship beyond (b)
?
推荐答案
DETACH DELETE在这里将很有用.这首先删除节点中的所有关系,然后删除节点本身.这使您的查询更加轻松,因为您只需查询b节点可访问的所有节点即可.
DETACH DELETE is going to be useful here. This first deletes all relationships from a node, then deletes the node itself. That makes your query easier, as all you need is a query for all nodes reachable from your b node.
我暂时要假设您问题中的这个b
节点是一个特定节点,而不是每个带有标签:b
的单个节点.我确实鼓励您重新阅读有关变量和标签的开发人员文档,因为我猜这里有些混乱.
I'm going to assume for the moment that this b
node in your question is a specific node, instead of every single node with the label :b
. I do encourage you to reread the developer documentation on variables and labels, as I'm guessing there's a little confusion here.
因此,假设有一个特定的b节点,并假定它具有区分它的name属性,则可以使用此查询将其删除,并将整个子图连接到该节点并可以从中进行访问.
So, assuming a specific b node, and assuming that it has a name property that differentiates it, you might use this query to delete it and the entire subgraph connected to it and reachable from it.
MATCH (b:b)-[*0..]-(x)
WHERE b.name = 'b'
WITH DISTINCT x
DETACH DELETE x
请注意,因为我们不在乎关系类型,并且因为我们指定了0个或多个关系,所以无论有多少关系,x都会与b及其整个连接的子图匹配.分离和删除x将删除子图中的所有关系,然后删除子图中的所有节点.
Note that because we don't care about the relationship type, and because we've specified 0 or more relationships, x will match to b and its entire connected subgraph no matter how many relationships away. Detaching and deleting x will delete all relationships in the subgraph and then all nodes in the subgraph.
这篇关于Neo4j:如何删除节点以外的所有节点和关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!