cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Count 2 relationships in a single cypher query

rounakshrestha1
Node Link

match (a:User {name: "HakumanaTata"}) ,(a)<-[f:Follows]-(:User) ,(a)-[F:Follows]->(:User) return count(f) as Followers, count(F) as Following

This returns nothing.
Also, which is faster, counting relationships or counting nodes?

1 ACCEPTED SOLUTION

dana_canzano
Neo4j
Neo4j

does

match (a:User {name: "HakumanaTata"}) return a;

report any data? if not then no such :User node exists.

if you run

match (a:User {name: "HakumanaTata"}) 
return size   (  (a)-[:Follows]->()  ) as Followers,
            size   (  (a)<-[:Follows]-()   )  as Following

should be fast and do what I suspect what you might be after however this is dependent on the fact that the :Follows relationship only connects to :User nodes.

See Fast counts using the count store - Knowledge Base for more details

View solution in original post

2 REPLIES 2

dana_canzano
Neo4j
Neo4j

does

match (a:User {name: "HakumanaTata"}) return a;

report any data? if not then no such :User node exists.

if you run

match (a:User {name: "HakumanaTata"}) 
return size   (  (a)-[:Follows]->()  ) as Followers,
            size   (  (a)<-[:Follows]-()   )  as Following

should be fast and do what I suspect what you might be after however this is dependent on the fact that the :Follows relationship only connects to :User nodes.

See Fast counts using the count store - Knowledge Base for more details

rounakshrestha1
Node Link

Thank you very much.. I don't know why the previous query did not execute correctly... the user I searched for definitely exists., fortunately for me your solution worked.