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.

How many times each node collaborated with other nodes

All what I need is knowing how many times each node collaborated with other nodes , I found this example and I followed it .
I runned this query :

MATCH (n:Attributaires)-[r:CollaborateWith]->(m:Attributaires)
With distinct n.id as Attributaires , size((n)-->(m)) AS NumberOfRelations , m.id AS Collaborators
RETURN Attributaires , NumberOfRelations , Collaborators

But I am getting the same value for all "NumberOfRelations" which is "1" .
Anyone has a solution and could help please ?

1 ACCEPTED SOLUTION

Based on the example you reference your issue my be with size((n)-->(m)).

In the example, @Benoit uses size((n)-->()). In this example leaving the second node "emtpy" () lets you count all of those that are connected. However, in your example, using (m) means you are only counting the relationships between (m) and (n). Unless you have multiple relationships connecting these nodes, you will always get "1".

Try using

MATCH (n:Attributaires)-[r:CollaborateWith]->(m:Attributaires)
With distinct n.id as Attributaires , size((n)-->()) AS NumberOfRelations , m.id AS Collaborators
RETURN Attributaires , NumberOfRelations , Collaborators

View solution in original post

3 REPLIES 3

Based on the example you reference your issue my be with size((n)-->(m)).

In the example, @Benoit uses size((n)-->()). In this example leaving the second node "emtpy" () lets you count all of those that are connected. However, in your example, using (m) means you are only counting the relationships between (m) and (n). Unless you have multiple relationships connecting these nodes, you will always get "1".

Try using

MATCH (n:Attributaires)-[r:CollaborateWith]->(m:Attributaires)
With distinct n.id as Attributaires , size((n)-->()) AS NumberOfRelations , m.id AS Collaborators
RETURN Attributaires , NumberOfRelations , Collaborators

I think you can simplify you query:

MATCH (n:Attributaires)-[r:CollaborateWith]->(m:Attributaires)
WITH n.id as Attributaires , m.id AS Collaborators, count(*) AS NumberOfRelations 
RETURN Attributaires , NumberOfRelations , Collaborators

This query give me the same results . But @mckenzma query worked 🙂