Neo4j 双向关系 [英] Neo4j Bidirectional Relationship
问题描述
有没有办法使用 Cypher 在 Neo4j 中创建双向关系?我希望关系是双向的,而不是在两个方向上建立两个单向关系例如:
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)
而不是:
(A)-[FRIEND]->(B)
(A)<-[FRIEND]-(B)
提前致谢:)
推荐答案
不,没有.neo4j 中的所有关系都有一个方向,从给定节点开始和结束.
No, there isn't. All relationships in neo4j have a direction, starting and ending at a given node.
有少量解决方法.
首先,正如你所建议的,我们可以有两种关系,一种是从 A 到 B,另一种是从 B 到 A.
Firstly, as you've suggested, we can either have two relationships, one going from A to B and the other from B to A.
或者,在编写 MATCH 查询时,我们可以指定无方向地匹配模式,通过使用诸如
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
不会关心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.
这篇关于Neo4j 双向关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!