update relationship property if previous relationship exists in NEO4J

拟墨画扇 提交于 2019-12-10 21:07:14

问题


I want to construct a query in cypher that does the following:

  • create a relationship between 2 nodes with a property containing a time duration if no relationship exists already
  • if a relationship is already present, update the time duration property to be the shortest duration of the two relationships

The compare and update part is something that I am not able to achieve in cypher. So any assistence on this part is greatly appreciated.


回答1:


You need combine MERGE ON CREATE | ON MATCH and CASE expression. For example:

MATCH (A:City {id: 1})
MATCH (B:City {id: 2})
WITH A, B, toInteger(rand()*100) as newDuration
MERGE (A)-[r:next]->(B)
  ON CREATE SET r.duration = newDuration
  ON MATCH  SET r.duration = CASE 
                                 WHEN r.duration > newDuration 
                                 THEN newDuration 
                                 ELSE r.duration 
                             END
RETURN r.duration


来源:https://stackoverflow.com/questions/42716898/update-relationship-property-if-previous-relationship-exists-in-neo4j

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