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.

Number of Indound relationships for a node?

size((n)<--())

If the above is depreciated so what's the best Cypher way in 4.4.4 to know the number of inbound or outbound relationships for a node?

8 REPLIES 8

How would you use count() to count the number of inbound relationships for a given node instead of counting the number of nodes itself which is I think two completly different things.

Counter the number of relationships for a node seems to be way faster to me than traversing the nodes connected then count them.

Where did you see that feature was deprecated in 4.4.4? I look at the list of deprecated features for 4.4.4 and did not see it. It is also listed on the latest cypher reference card.

The neo4j desktop shows up a warning when you use size with a pattern.

It looks like the future syntax will be something like this:

MATCH (a)
RETURN size([(a)-->(b) | 1]) AS years

Regards,
Cobra

yeah, I see that now. Your question is good. It was very logical and compact notation. You can use the pattern comprehension, as noted by @bennu.neo, or a subquery (which is very verbose), or an optional match, as below. Each of these solutions seems a little convoluted compared to size(pattern).

match(n:Item{key:340})
optional match (n)-->(r)
return n, count(r) as outCount

Any other ideas?

OPTIONAL MATCH doesn't count the number of in and/or out relationships from a node, it actually traverse each node connected to it and generate rows per connected node which is way slower than size().

I think Neo4j should reconsider using size for pattern as it's the most straighforward way to count the number of in or out relation's type.

I agree..It is very intuitive, straightforward, and direct. The other methods proposed seem like workarounds and indirectly figure it out.