尝试运行以下查询:
MATCH(u:User)
OPTIONAL MATCH (u)-[:USER_TYPE]->(adminType:UserType {type:'Admin'})
OPTIONAL MATCH (u)-[:USER_TYPE]->(userType:UserType {type:'User'})
RETURN DISTINCT { adminCount: count(adminType), userCount: count(userType) }
这会返回管理员的计数,但是用户是0.如果我切换OPTIONAL MATCH并将用户设置为第一,那么我得到用户的计数,但不是管理员。我如何得到两者?
答案 0 :(得分:2)
而不是使用OPTIONAL MATCH
试试这个:
MATCH (u:User)-[:USER_TYPE]->(adminType:UserType {type:'Admin'})
WITH count(*) AS adminCount
MATCH (u:User)-[:USER_TYPE]->(userType:UserType {type:'User'})
WITH count(*) AS userCount, adminCount
RETURN {adminCount: adminCount, userCount: userCount}
匹配每个模式,然后使用WITH
子句计算匹配数,以仅计算计数。
修改强>
正如@cybersam所指出的,上述查询考虑了关系的数量,以获得类型属性值为“Admin”和“User”的UserType
个节点的计数(不考虑关系) :
MATCH (adminType:UserType {type:'Admin'}) WITH count(adminType) AS adminCount
MATCH (userType:UserType {type:'User'}) WITH adminCount, count(userType)
RETURN {adminCount: adminCount, userCount: userCount}