问题描述
我正在尝试删除Neo4j中的叶子节点,但仅删除那些具有单一传入关系的叶子节点. (我好近.)
I am trying to remove leaf nodes in Neo4j, but only those with a single incoming relationship. (I'm so close.)
我有一个查询,返回要删除的确切节点.但是,当我用DELETE替换RETURN时,它删除的内容多于查询返回的内容.这是完整的序列:
I have a query that returns the exact node I wish to remove. However, when I replace the RETURN with DELETE, it removes more than the query returns. Here's the complete sequence:
neo4j-sh (?)$ match (n)-[r]->(p) return n, r, p ;
+------------------------------------------------------------+
| n | r | p |
+------------------------------------------------------------+
| Node[2164]{name:"a"} | :has[2616]{} | Node[2165]{name:"b"} |
| Node[2164]{name:"a"} | :has[2617]{} | Node[2166]{name:"c"} |
| Node[2166]{name:"c"} | :has[2619]{} | Node[2168]{name:"e"} |
| Node[2167]{name:"d"} | :has[2618]{} | Node[2165]{name:"b"} |
+------------------------------------------------------------+
此查询是完美的:
neo4j-sh (?)$ match ()-[r:has]->(n)
> with n,count(r) as rel_cnt
> where rel_cnt = 1 and NOT (n)-->()
> return n.name, rel_cnt;
+------------------+
| n.name | rel_cnt |
+------------------+
| "e" | 1 |
+------------------+
但是此删除操作删除了2个节点和3个关系吗?
But this delete removed 2 nodes and 3 relationships?
neo4j-sh (?)$ match ()-[r:has]->(n)
> with n, r, count(r) as rel_cnt
> where rel_cnt = 1 and NOT (n)-->()
> delete n, r;
+-------------------+
| No data returned. |
+-------------------+
Nodes deleted: 2
Relationships deleted: 3
这就是剩下的
neo4j-sh (?)$ match (n)-[r]->(p) return n, r, p ;
+------------------------------------------------------------+
| n | r | p |
+------------------------------------------------------------+
| Node[2164]{name:"a"} | :has[2617]{} | Node[2166]{name:"c"} |
+------------------------------------------------------------+
neo4j-sh (?)$ match (n) return n;
+----------------------+
| n |
+----------------------+
| Node[2169]{name:"a"} |
| Node[2171]{name:"c"} |
| Node[2172]{name:"d"} |
+----------------------+
为什么节点"b"被删除了?它没有显示在查询结果中.
Why was node 'b' removed? It didn't show in the query results.
推荐答案
除RETURN/DELETE
之外,查询实际上也不相同.返回查询将n, count(r)
携带到第二个查询部分,删除查询将n, r, count(r)
携带.尝试返回删除查询以查看该内容,即运行此
The queries are actually not identical even apart from RETURN/DELETE
. The return query carries n, count(r)
to the second query part, the delete query carries n, r, count(r)
. Try returning the delete query to see that, i.e. run this
neo4j-sh (?)$ match ()-[r:has]->(n)
> with n, r, count(r) as rel_cnt
> where rel_cnt = 1 and NOT (n)-->()
//> delete n, r;
> return *;
您会得到类似的东西
+-----------------------------------------------+
| n | r | rel_cnt |
+-----------------------------------------------+
| Node[2165]{name:"b"} | :has[2616]{} | 1 |
| Node[2165]{name:"b"} | :has[2618]{} | 1 |
| Node[2168]{name:"e"} | :has[2619]{} | 1 |
+-----------------------------------------------+
产生不同结果的原因是管道n, count(r)
的含义类似于每n个r r",只有一种情况每n个r r = 1".但是另一条管道的含义类似于每n个和每个r计数r",如果您自己对某事进行计数或分组,那么每次都将是一个. 未被删除的原因是它从未匹配或未被其他过滤器标准(NOT (n)-->()
)排除,rel_cnt=1
变得无用.
The reason for the different result is that piping n, count(r)
means something like "count r per n", and there is only one case where "count r per n = 1". But the other pipe means something like "count r per n and per r" and if you count or group something by itself, then it's going to be one every time. The reason something is not deleted is that it was never matched or is excluded by the other filter critera (NOT (n)-->()
), the rel_cnt=1
is rendered useless.
如果您想先计算这些关系然后有条件地删除它们,则可以收集它们,过滤收集的大小,然后从收集中删除.尝试类似
If you want to first count the relationships and then conditionally delete them, you can collect them, filter on collection size, and then delete from the collection. Try something like
MATCH ()-[r:has]->(n)
WITH n, collect(r) as rr
WHERE length(rr) = 1 AND NOT n-->()
FOREACH (r IN rr | DELETE r)
DELETE n
这篇关于在neo4j中删除具有单一关系的叶节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!