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

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

问题描述

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

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天全站免登陆