my match command is creating new nodes instead of matching the relationship with existing nodes

huangapple go评论68阅读模式
英文:

my match command is creating new nodes instead of matching the relationship with existing nodes

问题

我是初学者,正在尝试学习NEO4J。

首先,在NEO4J上执行了以下查询,并成功执行:

create(c:Company{CompanyCIN:"L01132WB1914PLC002502"})
create(d:Director{DirectorDIN:'434073'})

但是,当我运行一个查询来匹配或建立关系时,它创建了2个新节点。

match(c:Company{CompanyCIN:'L01132WB1914PLC002502'}),(d:Director{DirectorDIN:'434073'}) create (a)-[:Directed_by]->(b)

请问有人可以指导我,为什么在NEO4J中会发生这种情况?

my match command is creating new nodes instead of matching the relationship with existing nodes

英文:

I am a beginner and trying to learn NEO4J.

first, I executed the below query on NEO4J and it was successfully executed:

create(c:Company{CompanyCIN:"L01132WB1914PLC002502"})
create(d:Director{DirectorDIN:'434073'})

But when I run a query to match it or to make a relationship, it creates 2 new nodes.

match(c:Company{CompanyCIN:'L01132WB1914PLC002502'}),(d:Director{DirectorDIN:'434073'}) create (a)-[:Directed_by]->(b)

Can anyone please guide me, why this is happening in NEO4J?

my match command is creating new nodes instead of matching the relationship with existing nodes

答案1

得分: 2

在CREATE语句中的别名(a和b)与MATCH语句中的别名(c和d)不同。

只需更新查询为:

MATCH(c:Company{CompanyCIN:'L01132WB1914PLC002502'}),(d:Director{DirectorDIN:'434073'})
CREATE (c)-[:Directed_by]->(d)
英文:

The aliases in the CREATE statement (a and b) are different than those in the MATCH statement (c and d).

Just update the query to:

MATCH(c:Company{CompanyCIN:'L01132WB1914PLC002502'}),(d:Director{DirectorDIN:'434073'})
CREATE (c)-[:Directed_by]->(d)

答案2

得分: 1

@julielinx的回答是有效的,但如果您多次运行查询,将会在这两个节点之间创建多个Directed_by关系。

为了避免这种情况,您应该使用MERGE而不是CREATE,如下所示:

MATCH (c:Company{CompanyCIN:'L01132WB1914PLC002502'}), (d:Director{DirectorDIN:'434073'})
MERGE (c)-[:Directed_by]->(d)

英文:

@julielinx's answer is valid, but if you run the query multiple times you will end up with multiple Directed_by relationships between those 2 nodes.

To avoid that, you should use MERGE instead of CREATE, as in:

MATCH (c:Company{CompanyCIN:'L01132WB1914PLC002502'}), (d:Director{DirectorDIN:'434073'})
MERGE (c)-[:Directed_by]->(d)

huangapple
  • 本文由 发表于 2020年1月6日 21:51:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定