Cypher: How to return a node if the relationship doesn't exist?

南笙酒味 提交于 2019-12-11 12:32:43

问题


I have User and Building labels for nodes, the relationships are User-[:HAS_PERMISSION]->Building

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..)


回答1:


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


来源:https://stackoverflow.com/questions/22484598/cypher-how-to-return-a-node-if-the-relationship-doesnt-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!