使用标签和属性创建唯一 [英] CREATE UNIQUE with labels and properties

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

问题描述

我正在使用Neo4j 2.0.0-M06.只需学习Cypher并阅读文档即可.在我看来,这个查询是可行的,但是我应该很幸运...

I'm using Neo4j 2.0.0-M06. Just learning Cypher and reading the docs. In my mind this query would work, but I should be so lucky...

我正在将tweet导入到mysql-database,然后从那里将其导入neo4j.如果Neo4j数据库中已经存在一条推文,则应该对其进行更新.

I'm importing tweets to a mysql-database, and from there importing them to neo4j. If a tweet is already existing in the Neo4j database, it should be updated.

我的查询:

MATCH (y:Tweet:Socialmedia) WHERE
HAS (y.tweet_id) AND y.tweet_id = '123'
CREATE UNIQUE (n:Tweet:Socialmedia {
 body : 'This is a tweet', tweet_id : '123', tweet_userid : '321', tweet_username : 'example'
} )

Neo4j说:This pattern is not supported for CREATE UNIQUE

具有匹配标签的节点上的数据库当前为空,因此没有Neo4j数据库中的推文.

The database is currently empty on nodes with the matching labels, so there are no tweets what so ever in the Neo4j database.

正确的查询是什么?

推荐答案

您要对该查询使用MERGE,以及唯一约束.

You want to use MERGE for this query, along with a unique constraint.

CREATE CONSTRAINT on (t:Tweet) ASSERT t.tweet_id IS UNIQUE;

MERGE (t:Tweet {tweet_id:'123'})
ON CREATE
SET t:SocialMedia, 
    t.body = 'This is a tweet', 
    t.tweet_userid = '321', 
    t.tweet_username = 'example';

这将使用索引通过id查找该tweet,如果该tweet存在,则不执行任何操作,否则它将设置这些属性.

This will use an index to lookup the tweet by id, and do nothing if the tweet exists, otherwise it will set those properties.

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

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