Neo4j Cypher 查询 null 或 IN

Neo4j Cypher query null or IN

我有以下密码查询:

MATCH (parentD)-[:CONTAINS]->(childD:Decision)-[ru:CREATED_BY]->(u:User) 
WHERE id(parentD) = {decisionId}  
RETURN ru, u, childD 
SKIP 0 LIMIT 100

Decision 实体可以属于 0..N Tenant 个对象

@NodeEntity
public class Decision {
    private final static String BELONGS_TO = "BELONGS_TO";

    @Relationship(type = BELONGS_TO, direction = Relationship.OUTGOING)
    private Set<Tenant> tenants = new HashSet<>();

....

}

我需要扩展上面的 Cypher 查询,以便 return 所有 childD 其中 parentDchildD 不属于任何 Tenant 或属于 Tenant,ID 在 {tenantIds} 集合中提供。请帮我解决这个问题。

使用optional match收集测试tenants:

MATCH (parentD) WHERE id(parentD) = {decisionId}
  OPTIONAL MATCH (parentD)-[:BELONGS_TO]->(T:Tenant) 
           WHERE NOT id(T) IN {tenantIds}
WITH parentD, collect(T) AS TC 
     WHERE size(TC) <= 0
MATCH (parentD)-[:CONTAINS]->(childD:Decision)-[ru:CREATED_BY]->(u:User) 
  OPTIONAL MATCH (childD)-[:BELONGS_TO]->(T:Tenant)
           WHERE NOT id(T) IN {tenantIds}
WITH childD, ru, u, collect(T) AS TC
     WHERE size(TC) <= 0
RETURN ru, u, childD 
SKIP 0 LIMIT 100

Cypher 是一种非常有表现力的语言,按照你的文字要求...

MATCH (t:Tenant) WHERE ID(t) in {tenantIds}
WITH COLLECT(t) as tenants
MATCH (parentD)-[:CONTAINS]->(childD:Decision)-[ru:CREATED_BY]->(u:User) 
WHERE 
id(parentD) = {decisionId} 
AND
  // not belong to any of Tenant or belong to Tenant
  (not (parentD)-[:BELONGS_TO]-(:Tenant) OR  any(t in tenants WHERE (parentD)-[:BELONGS_TO]-(t)))
AND
  // not belong to any of Tenant or belong to Tenant 
  (not (childD)-[:BELONGS_TO]-(:Tenant)  OR  any(t in tenants WHERE (childD)-[:BELONGS_TO]-(t)))
RETURN ru, u, childD 
SKIP 0 LIMIT 100