Make new edge between two nodes if at least three broker nodes exist

I have a graph and I'm working with connections between nodes A and B with a broker node X as follows:

Now I need to add a new edge between A and B in a case that there are at least 3 distinct broker nodes X, e.g.:


I'm failing at the match part while I do not know how to properly count the broker nodes X and how to add it into the where part of the query, my try:

match p=(A)-[]-(X)-[]-(B) 
count(X) > 3
return p

but this does not work and I understand that it won't work like this. Can you help me please?

Thank you very much in advance.

Hi @ldpubsec


The code is not cool but work.

MATCH (a:A)-[]-(X)-[]-(b:B)
WITH a,b,count(X) AS countx
MATCH p=(a)-[]-()-[]-(b)
  WHERE countx > 3

1 Like

Thank you! This works!

May I ask how does it work? I'm a little puzzled by the double match, the first is ok with the following with, but why is there the second match? Thank you in advance.

If you don't need the return value of p, how about this?

MATCH (a:A)-[]-(X)-[]-(b:B)
WITH a,b,count(X) AS countx
  WHERE countx > 3