简体   繁体   English

Neo4j Cypher:从结果中排除某些节点

[英]Neo4j Cypher: exclude certain nodes from result

I have two queries, call them query A and query B. What I need is to get all the results of query A that are not in query B. Example:我有两个查询,分别称为查询 A 和查询 B。我需要的是获取查询 A 中不在查询 B 中的所有结果。示例:

Query A:查询一:

MATCH (m:MyNode {prop: 'value'}), (n:MyNode {prop: 'value', otherProp: (m).otherProp}
  WHERE m<>n AND shortestPath( (m)-[*]-(n) ) IS NULL
  RETURN m

Query B:查询 B:

MATCH (m:MyNode)-[:SOME_RELATION]->()<-[:SOME_RELATION]-(n:MyNode {prop: 'value'})
    WHERE m.prop<>'value'
    RETURN n

These two queries return what I expect, but what I really want is everything that Query A returns that Query B does not.这两个查询返回我期望的内容,但我真正想要的是查询 A 返回而查询 B 没有返回的所有内容。

I've tried a bunch of different things mostly using WHERE NONE based on what I found at https://neo4j.com/developer/kb/performing-pattern-negation/ , but all I seem to be able to do is eliminate all of the results instead of just those that come from Query B.根据我在https://neo4j.com/developer/kb/performing-pattern-negation/上找到的内容,我尝试了很多不同的东西,主要使用 WHERE NONE,但我似乎能做的就是消除所有结果,而不仅仅是来自查询 B 的结果。

Any suggestions?有什么建议?

Of course, after asking, I found my own solution.当然,经过询问,我找到了自己的解决方案。 Here's what I came up with:这是我想出的:

Match (r:MyNode)-[:SOME_RELATION]->()<-[:SOME_RELATION]-(s:MyNode { prop: 'value' })
    WHERE r.prop<>'value'
    WITH COLLECT(s) AS excluded
MATCH (m:MyNode {prop: 'value'}), (n:MyNode {prop: 'value', otherProp: (m).otherProp})
    WITH excluded, COLLECT(m) AS included
RETURN FILTER(n IN included WHERE NOT n IN EXCLUDED)

This works for me, but I'm certainly open to more efficient solutions if anyone has one.这对我有用,但如果有人有更有效的解决方案,我当然愿意接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM