gpt4 book ai didi

java - 如何避免重复节点?

转载 作者:行者123 更新时间:2023-11-30 01:51:01 26 4
gpt4 key购买 nike

我正在编写一个java项目,使用密码查询在neo4j中插入数据。我想阻止 neo4j 创建重复节点,而不是将另一个节点与现有节点链接。

CREATE (n1:node {name:'Adam'})-[:born_in]->(n2:node {name:'USA'})
//and again
CREATE (n1:node {name:'Adam'})-[:worked_at]->(n2:node {name:'Apple'})

我想创建一个 Adam 节点和另外两个节点,即 USAApple。 java中是否有任何检查来避免重复?

最佳答案

best way to create relationship between two nodes is

first get the two nodes.if they doesn't exists then create. Once the nodes are loaded, then simply create a relationship between them

根据您的情况,先使用MERGE 创建节点,然后使用关系

MERGE (n1:node {name:'Adam'})
MERGE (n2:node {name:'USA'})
MERGE (n1)-[r:born_in]->(n2)

此链接将帮助您理解MERGE

https://neo4j.com/docs/cypher-manual/current/clauses/merge/

关于java - 如何避免重复节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56071228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com