如何使用用Cypher创建唯一 [英] How to use Create Unique with Cypher

查看:128
本文介绍了如何使用用Cypher创建唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建节点+在不存在的情况下为其设置新属性

My target is to create node + set new property to it in case not exists

如果存在,我只想更新它的属性

if it's exist I just want to update it's property

尝试过:

MATCH (user:C9 {userId:'44'})
CREATE UNIQUE (user{timestamp:'1111'})
RETURN user

*如果具有属性userId = 44的节点已经存在,我只想将其属性设置为1111,否则只需创建并设置它即可.

*in case the node with the property userId=44 already existed I just want to set it's property into 1111 else just create it and set it.

我遇到的错误:

user already declared (line 2, column 16 (offset: 46))
"CREATE UNIQUE (user{timestamp:'1111'})"

我应该切换到合并"还是?

should I switch to Merge or?

谢谢.

推荐答案

是的,您应该使用MERGE语句.

Yes, you should use the MERGE statement.

MERGE (user:C9 {userId:'44'})
// you can set some initial properties when the node is created if required
//ON CREATE SET user.propertykey = 'propertyvalue' 
ON MATCH SET user.timestamp = '1111'
RETURN user

您提到了独特的约束-我假设您已经设置了一个约束.您绝对应该防止创建重复的节点.它还将创建一个架构索引,以提高节点查找的性能.如果您还没有唯一约束,则可以这样创建

You mention unique constraints - I assume you have one set up. You definitely should do to prevent duplicate nodes being created. It will also create a schema index to improve the performance of your node lookup. If you do not yet have a unique constraint then it can be created like so

CREATE CONSTRAINT ON (u:C9) ASSERT u.userId IS UNIQUE

请参见 Neo4j MERGE文档.

最后,要了解查询中发生的情况,让我们逐行快速浏览.

Finally, to understand what is going on in your query let's have a quick look line by line.

MATCH (user:C9 { userId:'44' })

这将匹配具有标签:C9的节点,该节点具有值为44userId属性,并为其分配标识符user.

This matches the node with label :C9 that has a userId property with value 44 and assigns it the identifier user.

CREATE UNIQUE (user{timestamp:'1111'})

此行只是试图创建一个没有标签且值为值'1111'的属性timestamp的新节点.您看到的异常是由于您使用了已经在第一行中使用过的同一user标识符的结果.但是,这不是使用CREATE UNIQUE的受支持方式,因为它首先需要匹配,然后会创建不存在的模式位.这样做的好处是,它将阻止在图形中创建此不需要的节点(user{timestamp:'1111'}).

This line is simply trying to create a new node with no label and a property timestamp with value '1111'. The exception you are seeing is a result of you using the same user identifier that has already been used in the first line. However, this is not a supported way to use CREATE UNIQUE as it requires a match first and will then create bits of the pattern that does not exist. The upside of this is that it is stopping this unwanted node (user{timestamp:'1111'}) being created in the graph.

RETURN user

这行很容易解释,没有到达.

This line is pretty self explanatory and is not being reached.

编辑 CREATE UNIQUE周围以及应该何时使用它似乎有些混乱.此查询

EDIT There seems to be some confusion surrounding CREATE UNIQUE and when it should be used. This query

CREATE UNIQUE (user:C9 {timestamp:'1111'})

将失败,并显示以下消息

will fail with the message

CREATE UNIQUE不支持此模式

This pattern is not supported for CREATE UNIQUE

要使用CREATE UNIQUE,您将首先匹配现有节点,然后使用该节点在图形中创建唯一的模式.因此,要创建从user到第二个节点的关系,您将使用

To use CREATE UNIQUE you would first match an existing node and then use that to create a unique pattern in the graph. So to create a relationship from user to a second node you would use

MATCH (user:C9 { userId: '44' }
CREATE UNIQUE (user)-[r:FOO]-(bar)
RETURN r

如果从user开始没有任何类型为FOO的关系,则将创建一个新的节点来表示bar,并且将在它们之间创建类型为:FOO的关系.相反,如果MATCH语句不匹配,则不会创建任何节点或关系.

If there is no relationships of type FOO from user then a new node will be created to represent bar and relationship of type :FOO will be created between them. Conversely, if the MATCH statement does not make a match then no nodes or relationships will be created.

这篇关于如何使用用Cypher创建唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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