Cypher returns twice for nodes that are connected in a bidirectional ratio

I have 2 nodes: (A), (B) related [: FRIEND]

When I run the following command,

start n = node(*) match (n)-[r:FRIEND]-(b) return n.name, b.name; 

it returns 2 rows: A, B and B, A.

I wonder how to make it return only one record, because the relation is bi-directional, A - [: FRIEND] -B and B - [: FRIEND] -A is considered the same result.

Thanks.

+4
source share
1 answer

One trick is to add where to the identifiers so that you also get them in sequential order:

 start n = node(*) match (n)-[r:FRIEND]-(b) where id(n) < id(b) return n.name, b.name; 

http://console.neo4j.org/r/1ry0ga

If you have several relationships between them (in both directions, for example), you can add a separate modifier to get the same results:

 start n = node(*) match (n)-[r:FRIEND]-(b) where id(n) < id(b) return distinct n.name, b.name; 
+9
source

All Articles