简体   繁体   中英

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:
both employees are calling each other and they are connected with each other by CALLED relationship..

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 :

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'].

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. 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).

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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