Neo4j中merge和create unique的区别 [英] Difference between merge and create unique in Neo4j

查看:36
本文介绍了Neo4j中merge和create unique的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚 MERGE 和 CREATE UNIQUE 之间的区别是什么.我知道这些功能:

I'm trying to figure out what is the difference between MERGE and CREATE UNIQUE. I know these features:

如果不存在模式,我可以创建节点.

I'm able to create node, if doesn't exist pattern.

    MERGE (n { name:"X" }) RETURN n;

这会创建具有属性名称的节点n"、空节点m"和相关关系.

This create node "n" with property name, empty node "m" and relationship RELATED.

    MERGE (n { name:"X" })-[:RELATED]->(m) RETURN n, m;

创造独一无二的

我无法像这样创建节点.

CREATE UNIQUE

I'm not able to create node like this.

    CREATE UNIQUE (n { name:"X" }) RETURN n;

如果存在节点n",则创建唯一的使空节点m"和关系相关.

If exists node "n", create unique makes empty node "m" and relationship RELATED.

    MATCH (n { name: 'X' }) CREATE UNIQUE (n)-[:RELATED]->(m) RETURN n, m;

如果此模式存在,则不创建任何内容,仅返回模式.

If this pattern exists, nothing created, only returns pattern.

从我的角度来看,我看到 MERGE 和 CREATE UNIQUE 是完全相同的查询,但是使用 CREATE UNIQUE 你不能在关系中创建起始节点.如果有人可以解释这个问题并比较这些查询,我将不胜感激.

From my point of view, I see MERGE and CREATE UNIQUE are quite same queries, but with CREATE UNIQUE you can't create start node in relationship. I would be grateful, if someone could explain this issue and compare these queries, thx.

推荐答案

CREATE UNIQUE 的语义比 MERGE 稍微模糊一些.MERGE 被开发为一种比 CREATE UNIQUE 具有更直观行为的替代方案;如果有疑问,MERGE 通常是正确的选择.

CREATE UNIQUE has slightly more obscure semantics than MERGE. MERGE was developed as an alternative with more intuitive behavior than CREATE UNIQUE; if in doubt, MERGE is usually the right choice.

MERGE 视为 MATCH-or-create 的最简单方法.也就是说,如果数据库中的某些内容将 MATCH 您在 MERGE 中使用的模式,那么 MERGE 将只返回该模式.如果没有匹配项,MERGE 将创建模式中所有缺失的元素,其中缺失的元素意味着任何未绑定的标识符.

The easiest way to think of MERGE is as a MATCH-or-create. That is, if something in the database would MATCH the pattern you are using in MERGE, then MERGE will just return that pattern. If nothing matches, the MERGE will create all missing elements in the pattern, where a missing element means any unbound identifier.

给定

MATCH (a {uid:123})
MERGE (a)-[r:LIKES]->(b)-[:LIKES]->(c)

"a" 从 MERGE 的角度来看是一个绑定标识符.这意味着 cypher 不知何故已经知道它代表哪个节点.

"a" is a bound identifier from the perspective of the MERGE. This means cypher somehow already knows which node it represents.

这个语句可以有两种结果.要么整个​​模式已经存在,并且不会创建任何内容,或者模式的某些部分丢失,并且将创建一组全新的与该模式匹配的关系和节点.

This statement can have two outcomes. Either the whole pattern already exists, and nothing will be created, or parts of the pattern are missing, and a whole new set of relationships and nodes matching the pattern will be created.

示例

// Before merge:
(a)-[:LIKES]->()-[:LIKES]->()

// After merge:
(a)-[:LIKES]->()-[:LIKES]->()


// Before merge:
(a)-[:LIKES]->()-[:OWNS]->()

// After merge:
(a)-[:LIKES]->()-[:OWNS]->()
(a)-[:LIKES]->()-[:LIKES]->()


// Before merge:
(a)

// After merge:
(a)-[:LIKES]->()-[:LIKES]->()

这篇关于Neo4j中merge和create unique的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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