[英]How to delete a node and its connected nodes with Neo4j cypher query?
For example, I want to delete Actor
node with id = "005A" and its connected Movie
nodes. 例如,我要删除id =“ 005A”的
Actor
节点及其连接的Movie
节点。 The relationship between Actor and Movie node is ACTED_IN
. Actor和Movie节点之间的关系是
ACTED_IN
。
I have tried this cypher query: 我已经尝试过以下密码查询:
MATCH (a:Actor {id: "005A"})
OPTIONAL MATCH (a)-[r:ACTED_IN]->(m)
DELETE a, r, m;
but it didn't work, I got TransactionFailureException: Unable to commit transaction
error. 但它不起作用,我收到了
TransactionFailureException: Unable to commit transaction
错误。
Anyone can give a solution? 有人可以提供解决方案吗?
UPDATE: 更新:
I found out that there is relationship from other node ( Agency
) to Actor
node which labeled as OWNED
. 我发现从其他节点(
Agency
)到Actor
节点之间存在关系,标记为OWNED
。 So the diagram is like this: 因此该图是这样的:
(aa:Agency)-[o:OWNED]->(a:Actor)
[EDITED] [编辑]
You cannot delete a node unless all of its relationships have also been deleted. 除非还删除了所有节点的关系,否则您无法删除该节点。 In your case, it is possible that the
a
and/or m
nodes have relationships other than r
. 在您的情况下,
a
和/或m
节点可能具有除r
之外的关系。
To get the set of relationship types associated with a
and m
, you can use the following (I've limited the result to 10 rows, in case a
and/or m
have a lot of relationships`): 要获取与
a
和m
关联的a
组关系类型,可以使用以下命令(如果a
和/或m
有很多关联,我将结果限制为10行):
MATCH (a:Actor {id: "005A"})
OPTIONAL MATCH ()-[rx]-(a)
OPTIONAL MATCH (a)-[r:ACTED_IN]->(m)
OPTIONAL MATCH (m)-[ry]-()
RETURN a, r, m, COLLECT(DISTINCT TYPE(rx)), COLLECT(DISTINCT TYPE(ry))
LIMIT 10;
I suspect that your results will show types other than ACTED_IN
. 我怀疑您的结果将显示
ACTED_IN
以外的其他类型。
The following query should delete the actor and all movies s/he acted in: 以下查询应删除该演员和他/她参加过的所有电影:
MATCH (a:Actor {id: "005A"})
OPTIONAL MATCH ()-[rx]-(a)
OPTIONAL MATCH (a)-[r:ACTED_IN]->(m)
OPTIONAL MATCH (m)-[ry]-()
DELETE rx, ry, r, a, m;
Are you sure the id is encoded as a string property ? 您确定id编码为字符串属性吗? I guess the id of the movie database is an integer and thus the id should not be encapsulated in quotes :
我猜电影数据库的ID是一个整数,因此该ID不应封装在引号中:
MATCH (a:Actor {id: 5})
OPTIONAL MATCH (a)-[r:ACTED_IN]->(m)
DELETE a, r, m;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.