user157629
user157629

Reputation: 634

Cypher sort by amount of same relationship?

How can I match using Cypher by any relation amount? For example, in a database there are Person. If this person has any son, they will be related by the relation [:SON].

This query will return each Person that has a son:

match (p:Person)-[:SON]->(:Person)
return p

Knowing this, how can I match the number of Person that only has 2 sons?

Upvotes: 0

Views: 41

Answers (2)

Christoph P
Christoph P

Reputation: 133

You can use size() in the where clause, like so:

match (p:Person) where size((p)-[:SON]->())=2 return p

Upvotes: 2

Rajendra Kadam
Rajendra Kadam

Reputation: 4052

You can use the count function to get the count of sons for each person. Then filter out the persons based on the count.

MATCH (p:Person)-[r:SON]->(:Person)
WITH p, count(r) as sons 
WHERE sons=2
RETURN p

Upvotes: 1

Related Questions