我有一个以下的密码查询:
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
和parentD
不属于任何childD
或属于Tenant
的所有Tenant
使用{tenantIds}
集中提供的ID。请帮我解决这个问题。
答案 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