Neo4J Cypher - Count Relationships of Matched Nodes

安稳与你 提交于 2019-12-02 05:43:24

I think doing both of your OPTIONAL MATCHES back to back are resulting in some duplicate results (consider the output at each stage with the variables involved...multiple row matches for who each follower is following with a cross product to all the row matches of who is following each follower).

While you could fix this by assembling your data (getting the count) after each OPTIONAL MATCH, a better approach is to switch from using OPTIONAL MATCHES and instead get the number of relationships directly with the SIZE function:

MATCH (:User{id:2})<-[:Follows]-(followers)
RETURN followers.id, SIZE((followers)-[:Follows]->()) AS Follows, SIZE(()-[:Follows]->(followers)) AS Following
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!