Neo4j Cypher查询null或IN

时间:2017-01-21 14:02:58

标签: neo4j cypher spring-data-neo4j

我有一个以下的密码查询:

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查询,以便返回childDparentD不属于任何childD或属于Tenant的所有Tenant使用{tenantIds}集中提供的ID。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

使用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