在Cypher中,如果不存在关系,该如何创建关系?更新属性(如果有) [英] In Cypher, how can I create a relationship if it doesn't exist; update property if it does

查看:81
本文介绍了在Cypher中,如果不存在关系,该如何创建关系?更新属性(如果有)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Neo4J的Cypher中,给定两个节点,如果它们之间没有关系,我想创建一个权重属性为1的关系(类型为Foo).如果这种关系已经存在,我想增加其weight属性.

In Cypher in Neo4J, given two nodes, if there's no relationship between them, I'd like to create a relationship (of type Foo) with a weight property of one. If this relationship already exists, I'd like to increment its weight property.

在单个Cypher查询中是否有很好的方法来做到这一点?谢谢!

Is there a good way to do this in a single Cypher query? Thanks!

一些其他详细信息: 节点已经创建,唯一并且在索引中.

Some additional details: The nodes are already created, unique, and in an index.

推荐答案

这正是我们在1.8中添加CREATE UNIQUE的原因.

This is exactly why we added CREATE UNIQUE in 1.8.

START a=node(...), b=node(...)
CREATE UNIQUE a-[r:CONNECTED_TO]-b
SET r.weight = coalesce(r.weight?, 0) + 1

有关CREATE UNIQUE的更多信息此处,问号此处.

Read more about CREATE UNIQUE here, the question mark here, and coalesce here.

这篇关于在Cypher中,如果不存在关系,该如何创建关系?更新属性(如果有)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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