英文:
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中会发生这种情况?
英文:
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?
答案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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论