Neo4j Bidirectional Relationship

夙愿已清 提交于 2019-12-01 02:10:58

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.

According to this article: Modeling Data in Neo4j: Bidirectional Relationships

The strictly better choice is to create a relationship in an arbitrary direction and not specify the direction when querying:

MATCH (neo)-[:PARTNER]-(partner)

The engine is capable of traversing the edge in either direction. Creating the anti-directional edge is unnecessary and only serves to waste space and traversal time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!