如果NEO4J中存在先前的关系,则更新关系属性 [英] update relationship property if previous relationship exists in NEO4J

查看:1092
本文介绍了如果NEO4J中存在先前的关系,则更新关系属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在cypher中构造一个执行以下操作的查询:

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.

推荐答案

您需要组合 MERGE ON CREATE | ON MATCH CASE expression .例如:

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

这篇关于如果NEO4J中存在先前的关系,则更新关系属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆