【问题标题】:Neo4j Cypher query null or INNeo4j Cypher 查询 null 或 IN
【发布时间】:2017-01-21 14:02:58
【问题描述】:

我有以下密码查询:

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 查询以返回所有childD,其中parentDchildD 不属于Tenant 中的任何一个或不属于Tenant,ID 设置在{tenantIds} 中。请帮我解决这个问题。

【问题讨论】:

    标签: neo4j cypher spring-data-neo4j


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-18
        • 1970-01-01
        • 1970-01-01
        • 2015-11-01
        • 2017-12-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多