问题描述
我有节点的用户"和建筑物"标签,关系为User-[:HAS_PERMISSION]->Building
I have User and Building labels for nodes, the relationships are User-[:HAS_PERMISSION]->Building
要获取用户有权访问的所有建筑物,请使用MATCH (u:User {id: {id}}),(u)-[r:HAS_PERMISSION]->(b:Building) return b
To get all buildings that a user is having permissions to so I use MATCH (u:User {id: {id}}),(u)-[r:HAS_PERMISSION]->(b:Building) return b
如果用户存在并且没有关系,我该如何返回用户? (基本上,我也想知道用户是否存在.)
How can I just return the user in case he exists and there are no relationships? (basically I also want to know if the user exists at all..)
推荐答案
不确定我是否正确理解了您的问题,但我认为您需要OPTIONAL MATCH
.该查询显示所有有权访问或不访问建筑物的用户.
Not sure if I understand your question correctly, but I think you'll need an OPTIONAL MATCH
. This query shows all users with or without access to a building.
MATCH (u:User)
OPTIONAL MATCH (u)-[r:HAS_PERMISSION]->(b:Building)
RETURN u, b
或者您的要求可能更简单.如果只需要所有用户,则无需使用模式匹配:
Or maybe your requirement is simpler. If you just want all users, there's no need to use pattern matching:
MATCH (u:User)
RETURN u
这篇关于Cypher:如果关系不存在,如何返回节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!