Neo4 / Cypher:删除节点的所有关系

时间:2013-01-24 23:25:31

标签: neo4j cypher

我想删除节点的所有关系(传入和传出)(关系可以在许多不同的节点上进行)

类似的东西:

start n = node:(1000)MATCH n< - [r] - > anynode DELETE r

不起作用,因为这里“anynode”被解释为匹配的“第一个”链接节点。

有什么想法吗?

2 个答案:

答案 0 :(得分:13)

你想:

start n=node(1000) 
match n-[r]-() 
delete r;

答案 1 :(得分:0)

我在使用@ eve-freeman解决方案时遇到了一个问题,试图删除所有关系,作为更大更新操作的一部分(即删除现有关系,然后创建新关系,以及更新节点属性):

MATCH (n:Node { uuid: '1' })
OPTIONAL MATCH (n)-[r]-()
DELETE r
WITH n
MATCH (f:Foo { uuid: '2' })
CREATE (n)-[:LIKES]->(f)
WITH n
MATCH (b:Bar { uuid: '3' })
CREATE (n)<-[:LOVES]-(b)
SET n.name = 'Howard'
RETURN n

由于某种原因,它创建了一个重复的条目(我有兴趣找出这种情况发生的原因):

"n"
{"name":"Howard","uuid":"1"}
{"name":"Howard","uuid":"1"}
Set 2 properties, deleted 2 relationships, created 4 relationships, started streaming 2 records after 6 ms and completed after 6 ms.

使用以下(COLLECT然后FOREACH)似乎有效:

MATCH (n:Node { uuid: '1' })
OPTIONAL MATCH (n)-[r]-()
WITH n, COLLECT (r) AS rels
FOREACH (r IN rels | DELETE r)
WITH n
MATCH (f:Foo { uuid: '2' })
CREATE (n)-[:LIKES]->(f)
WITH n
MATCH (b:Bar { uuid: '3' })
CREATE (n)<-[:LOVES]-(b)
SET prd.name = 'Howard'
RETURN n

返回:

"n"
{"name":"Howard","uuid":"1"}
Set 1 property, deleted 2 relationships, created 2 relationships, started streaming 1 record after 3 ms and completed after 3 ms.