我有这个型号:
用户有一个名为 identityId (CONSTRAINT UNIQUE)的属性来识别用户。
我想选择具有给定id的User,只要它是用户本身(相同的identityId),或者它是否存在Alice和Bob之间的SHARED_WITH关系。
就性能而言,查询下面是最佳查询吗?
MATCH (u:User)
WHERE id(u) = {id} AND ((u.identityId = {identityId})
OR ((:User { identityId: {identityId} }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u)))
RETURN u
示例
第一种情况:来电者是Alice
MATCH (u:User)
WHERE id(u) = 6 AND ((u.identityId = "5678")
OR ((:User { identityId: "5678" }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u)))
RETURN u
你是爱丽丝
第二种情况:来电者是鲍勃
MATCH (u:User)
WHERE id(u) = 6 AND ((u.identityId = "1234")
OR ((:User { identityId: "1234" }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u)))
RETURN u
你是爱丽丝
第三种情况:来电者是Mallory
MATCH (u:User)
WHERE id(u) = 6 AND ((u.identityId = "2222")
OR ((:User { identityId: "2222" }) - [:OWNS] -> (:Clinic) <- [:SHARED_WITH] - (u)))
RETURN u
u为NULL(mallory既不是用户也不是Alice已共享其用户的用户)
答案 0 :(得分:0)
考虑到您的其他解释:
// Get user by id
MATCH (I:User) WHERE id(I) = {id}
// Whom with given {identityId} shard with him
OPTIONAL MATCH (U:User {identityId: {identityId} })
-[:OWNS]->()<-[:SHARED_WITH]-
(I)
WITH I, COUNT(U) as UC
// Test user {identityId}
// or there are those who with {identityId} are with him shares
WHERE I.identityId = {identityId} OR UC > 0
RETURN I