0
votes

I am a newbie who just started learning graph database and I have a problem querying the relationships between nodes.

My graph is like this:

enter image description here

There are multiple relationships from one node to another, and the IDs of these relationships are different.

How to find relationships where the number of relationships between two nodes is greater than 2,or is there a problem with the model of this graph?

Just like on the graph, I want to query node d and node a.

I tried to use the following statement, but the result is incorrect:

match (from)-[r:INVITE]->(to)
with from, count(r) as ref
where ref >2
return from

It seems to count the number of relations issued by all from, not the relationship between from-->to.

2

2 Answers

0
votes

to return nodes who have more then 2 relationship between them you need to check the size of the collected rels. something like

MATCH (x:Person)-[r:INVITE]-(:Party)
WITH x, size(collect(r)) as inviteCount
WHERE inviteCount > 2
RETURN x
0
votes

Aggregating functions like COLLECT and COUNT use non-aggregating terms in the same WITH (or RETURN) clause as "grouping keys".

So, here is one way to get pairs of nodes that have more than 2 INVITE relationships (in a specific direction) between them:

MATCH (from)-[r:INVITE]->(to)
WITH from, to, COUNT(r) AS ref
WHERE ref > 2
RETURN from, to

NOTE: Ideally (for clarity and efficiency), your nodes would have specific labels and the MATCH pattern would specify those labels.