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 relationship on each nodes

I have nodes with one of the properties called "Model".
If I have node names as a,b,c,d,e etc,
a.Model="1"
b.Model="2"
c.Model="3"
d.Model="1" etc

I want to ask a query : "For a particular Model, return the count of relationships for each individual nodes "

If i ask for Model=1 , it should give me the count of relationships for each distinct node under Model 1.
need results like below
for Model="1"
a---> count of relationships for a
d---> count of relationships for d

1 ACCEPTED SOLUTION

Benoit
Graph Buddy

Hi,

You can try this query :

MATCH (n:MyNodeLabel)
WHERE n.Model = "1"
RETURN n, size((n)-->()) AS count

NB : In this example, you should replace MyNodeLabel by the label of your nodes.

Cheers.

View solution in original post

8 REPLIES 8

Benoit
Graph Buddy

Hi,

You can try this query :

MATCH (n:MyNodeLabel)
WHERE n.Model = "1"
RETURN n, size((n)-->()) AS count

NB : In this example, you should replace MyNodeLabel by the label of your nodes.

Cheers.

what if I don't know that the value is "1" or "2" etc

and I want that it will recognize by itself and count it?

If you don’t want to filter by the value, you can leave out the where clause in get a count for all nodes.
if this is not what you meant, can you please describe what you are trying to accomplish?

I will try again-

I have a connection of multiple nodes and one of their properties is name, for the entire connections I want to count the appearance of each name, whatever it is.

Can you provide your data model? Do you want the total count of the number of nodes for each value of name, or is the restricted to the related nodes of a node?

I can't give the data..

the second option- restricted to the related nodes of a node

This is very generic since I don't have a data model. It shows how to count the number of related nodes by the name property for each node in your database. You will need to tailor it to your data model.

match(n)--(m)
return n, m.name, count(*) as cnt

Is this what you are looking for?

When you say "Node name" as a, b, c, etc. - what do you mean? Is there a property called "name" on the nodes?