Neo4J Cypher - Count Relationships of Matched Nodes

后端 未结 1 863
夕颜
夕颜 2021-01-25 08:52

I am working on a small project where I have to maintain follows between users like twitter. I am trying to make a query that returns the followers of a certain node, let\'s cal

相关标签:
1条回答
  • 2021-01-25 09:54

    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
    
    0 讨论(0)
提交回复
热议问题