【发布时间】:2015-03-24 13:10:38
【问题描述】:
在 SQL 中:
Delete From Person Where ID = 1;
在 Cypher 中,通过 ID 删除节点的脚本是什么?
(已编辑:ID = Neo4j 的内部节点 ID)
【问题讨论】:
标签: neo4j nosql cypher graph-databases
在 SQL 中:
Delete From Person Where ID = 1;
在 Cypher 中,通过 ID 删除节点的脚本是什么?
(已编辑:ID = Neo4j 的内部节点 ID)
【问题讨论】:
标签: neo4j nosql cypher graph-databases
假设您指的是 Neo4j 的内部节点 ID:
MATCH (p:Person) where ID(p)=1
OPTIONAL MATCH (p)-[r]-() //drops p's relations
DELETE r,p
如果您指的是节点上自己的属性“id”:
MATCH (p:Person {id:1})
OPTIONAL MATCH (p)-[r]-() //drops p's relations
DELETE r,p
【讨论】:
当节点是孤儿时。
Start n=node(1)
Delete n;
【讨论】:
对于 id 为“x”的节点,最干净的扫描是
匹配 (n) 其中 id(n) = x
分离删除 n
https://neo4j.com/docs/cypher-manual/current/functions/scalar/#functions-id
【讨论】:
按照@saad-khan 提供的链接,这是获取节点和关系 ID 的示例。 下面的代码显示了 ID,因此您可以确保删除与给定 ID 相关的所有内容。
MATCH (node)-[relation:HAS]->(value)
where ID(node)=1234
RETURN ID(instance), ID(value), ID(r)
Ps.:“:HAS”是关系的一个例子。
【讨论】:
老问题和答案,但要在有关系时删除节点,请使用DETACH
MATCH (n) where ID(n)=<your_id>
DETACH DELETE n
否则你会得到这个:
Neo.ClientError.Schema.ConstraintValidationFailed: Cannot delete node<21>, because it still has relationships. To delete this node, you must first delete its relationships.
就像 SQL 的CASCADE
【讨论】: