简体   繁体   English

如何在neo4j中为双向关系属性添加值的集合?

[英]How to add collection of values for a bidirectional relationship properties in neo4j?

I am creating 2 nodes in neo4j with a directional properties like below: 我正在neo4j中创建2个节点,其方向属性如下所示:
both employees are calling each other and they are connected with each other by CALLED relationship.. 两名员工互相呼叫,并通过CALLED关系彼此连接。

MATCH (e1:EMP),(e2:EMP) WHERE e1.NUMBER='200' AND e2.NUMBER='100' MERGE (e1)-[r:CALLED]->(e2) SET r.DURATION = ['233']    

and my result is like below. 我的结果如下所示。

在此处输入图片说明

when I am creating a relation in revrese direction : 当我在recrese方向上创建关系时:

MATCH (e1:EMP),(e2:EMP) WHERE e1.NUMBER='100' AND e2.NUMBER='200' MERGE (e1)-[r:CALLED]->(e2) SET r.DURATION = "235" +r.DURATION[0..]  

and my result is showing something strange 我的结果显示出一些奇怪的东西

在此处输入图片说明

How can I add two properties in a collection so that it should look like 如何在集合中添加两个属性,使其看起来像

在此处输入图片说明

The concept of bidirectional exists only when querying. 双向的概念仅在查询时存在。 When creating relationships, every relationship must have a direction. 创建关系时,每个关系都必须有一个方向。 What you have two different relations, one in each direction. 您有两个不同的关系,每个方向一个。 The relation from emp 200 to 100 has a property called duration with value ['233']. 从emp 200到100的关系具有一个名为duration的属性,其值为['233']。

Next, when you create a relation in the opposite direction from emp 100 to 200, this relation is a new one, it has nothing to do with the earlier relation except that the participating nodes are the same. 接下来,当您在从emp 100到200的相反方向上创建关系时,该关系是新关系,它与较早的关系无关,只是参与的节点相同。 In this query 在这个查询中

MATCH (e1:EMP),(e2:EMP) 
WHERE e1.NUMBER='100' AND e2.NUMBER='200' 
MERGE (e1)-[r:CALLED]->(e2) SET r.DURATION = "235" +r.DURATION[0..]  

r.DURATION is null because the DURATION property does not yet exist on the relationship r from e1(100) to e2(200). r.DURATION为空,因为从e1(100)到e2(200)的关系r上尚不存在DURATION属性。

If you want to add durations to the relationship in a specific direction, you could use something like this 如果要在特定方向上向关系添加持续时间,可以使用类似以下的方法

MATCH (e1:EMP),(e2:EMP) 
WHERE e1.number='100' AND e2.number='200' MERGE (e1)-[r:CALLED]->(e2) 
SET r.DURATION =["335"]+coalesce(r.DURATION,[])

Note that this inserts new duration values into the array on the relationship from emp 100 to 200. The values from emp 200 to 100 are unread and unmodified. 请注意,这会将新的持续时间值插入到emp 100到200之间的关系数组中。emp200到100之间的值是未读和未修改的。 If you wish to append values from the relationship in the opposite direction, you'll have to match it first to get the DURATION property. 如果希望以相反的方向从关系中追加值,则必须先对其进行匹配才能获得DURATION属性。 Doing this implies the same property value on the relation in both directions, and then I'd question why you need two relations instead of one. 这样做意味着双向上的关系具有相同的属性值,然后我要问为什么需要两个关系而不是一个。

If the direction is of no importance, use a single relation between e1 and e2. 如果方向无关紧要,请在e1和e2之间使用单个关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM