Neo4j Bidirectional Relationship

sgp picture sgp · Jun 3, 2014 · Viewed 14.2k times · Source

Is there a way to create bidirectional relationship in Neo4j using Cypher? I would like the relationship to be bidirectional rather than making two unidirectional relationships in both directions For eg:

(A)<-[FRIEND]->(B)

Rather than:

(A)-[FRIEND]->(B)
(A)<-[FRIEND]-(B)

Thanks in advance :)

Answer

David Simons picture David Simons · Jun 3, 2014

No, there isn't. All relationships in neo4j have a direction, starting and ending at a given node.

There are a small number of workarounds.

  • Firstly, as you've suggested, we can either have two relationships, one going from A to B and the other from B to A.

  • Alternatively, when writing our MATCH query, we can specify to match patterns directionlessly, by using a query such as

    MATCH (A)-[FRIEND]-(B) RETURN A, B
    

    which will not care about whether A is friends with B or vice versa, and allows us to choose a direction arbitrarily when we create the relationship.