有没有一种方法可以应用...在没有任何匹配项的情况下不影响原始匹配节点?
以下作品很棒。我现在试图在不更改n输出的情况下从o和s中排除一些结果。
MATCH (n) WHERE n.id='1'
OPTIONAL MATCH (n)-[:TYPE1]->(o:Label1)
OPTIONAL MATCH (n)-[:TYPE2]->(s:Label1)
WITH n, o, s
OPTIONAL MATCH (n)<-[:TYPE4]-(m)
RETURN distinct n.id as n_id, collect(distinct m.id) as m_ids, count(distinct m) as m_count, collect(distinct s.id)+collect(distinct o.id) as s_ids;
我想用此添加项过滤s和o:
WITH n, o, s, collect(distinct s)+collect(distinct o) as x, [(s:Label1)-[:TYPE3]->(:Label1) | s] as e
WHERE NONE (s in e WHERE s in x)
所以我最终得到了
MATCH (n) WHERE n.id='1'
OPTIONAL MATCH (n)-[:TYPE1]->(o:Label1)
OPTIONAL MATCH (n)-[:TYPE2]->(s:Label1)
WITH n, o, s, collect(distinct s)+collect(distinct o) as x, [(s:Label1)- [:TYPE3]->(:Label1) | s] as e
WHERE NONE (s in e WHERE s in x)
WITH n, o, s
OPTIONAL MATCH (n)<-[:TYPE4]-(m)
RETURN distinct n.id as n_id, collect(distinct m.id) as m_ids, count(distinct m) as m_count, collect(distinct s.id) as s_ids;
但这不会返回预期的n个节点。我期望在第一行找到的n个节点在最后返回n。对于找到的每个n个节点,s_ids均正确返回,但是使用WITH ... WHERE NONE(x中的e WHERE中的s)过滤找到的n个节点。当我需要WITH ...以收集列表时,如何将“ WHERE NONE(s in e WHERE s in x)”应用于可选匹配项(o和s),而不应用于匹配项本身(n) (从o和s)在何处运行(不包括某些o和s)?还是我完全以错误的方式来做这件事?是我在重用变量s吗?我尝试使用新变量,但无法运行(未知变量)。