cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Count 2 relationships in a single cypher query

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

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

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

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.