[英]Neo4j/Cypher Match only if predicate applies for all relationships
I need to match nodes only when every relationship the node has fullfills a whereclause: 我只需要在节点满足其中的每个关系时匹配节点:
MATCH (o:Otherthing)
WHERE id(o) = 1
MATCH (unknown:Thing)
WHERE (unknown)-[:DEPENDS_ON]->(:Thing)<-[:DEPENDS_ON*]-(:Thing)<-[:STARTED_WITH]-(o)
RETURN unknown
Every matched "Thing" should only have relationships labeled with "DEPENDS_ON" and all of them should fullfill the condition. 每个匹配的“Thing”应该只有标有“DEPENDS_ON”的关系,并且所有这些关系应该满足条件。
How can I achieve that? 我怎样才能做到这一点?
This may work for you: 这可能对你有用:
MATCH (u:Thing)-[:DEPENDS_ON]->(:Thing)<-[:DEPENDS_ON*]-(:Thing)<-[:STARTED_WITH]-(o)
WHERE ID(o) = 1
WITH u, COUNT(*) AS num
WHERE SIZE((u)--()) = num
RETURN u
The query uses an efficient degreeness check to get the total number of relationships for u
, and compares that with the number of times u
satisfied the MATCH
. 该查询使用一个高效degreeness检查,以获得关系的总数u
,并比较与次数u
满足MATCH
。 Also, since you are identifying o
by its native ID (which I am assuming is always going to be the ID for an Otherthing
), it is more efficient to not specify its label (to avoid a label verification operation). 此外,由于您通过其本机ID识别o
(我假设它始终是Otherthing
的ID),因此不指定其标签(避免标签验证操作)更有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.