Cypher 查询 MATCH (n:BusinessBranch) RETURN n
返回所有节点,我想根据属性 address
删除重复的节点及其关系。请问如何操作?
[更新]
要删除所有共享相同 address
属性值的 BusinessBranch
节点(这也需要删除它们的所有关系):
MATCH (b:BusinessBranch)
WITH b.address AS address, COLLECT(b) AS branches
WHERE SIZE(branches) > 1
FOREACH (n IN branches | DETACH DELETE n);
此查询收集所有具有相同地址的BusinessBranch
节点,过滤掉只有一个分支的集合,然后对结果集中的所有分支使用DETACH DELETE
(这将删除分支及其关系)。
要删除除一个之外的所有重复节点,您可以执行以下操作:
MATCH (b:BusinessBranch)
WITH b.address AS address, COLLECT(b) AS branches
WHERE SIZE(branches) > 1
FOREACH (n IN TAIL(branches) | DETACH DELETE n);
然而,在这种情况下,您应该首先查看APOC过程apoc.refactor.mergeNodes,它对于大多数用例更加合适。