imran fida
imran fida

Reputation: 25

How to avoid duplicate node?

I am writing a java project to insert data in neo4j using a cypher query. I want to stop neo4j from creating a duplicate node, instead linking the other node with the existing node.

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

I want to create one node of Adam and two other nodes i.e. USA and Apple. Are there any checks in java to avoid duplications?

Upvotes: 2

Views: 1319

Answers (2)

Jonathan
Jonathan

Reputation: 4689

For anyone looking for a general way to avoid adding duplicates:

MERGE (u:User { email: "[email protected]" }) 
SET u.name="Jon Smith" 
RETURN u

This works in both neo4j and redisgraph.

J

Upvotes: 0

Govind Singh
Govind Singh

Reputation: 15490

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

in your case, create your nodes with MERGE and then relationship

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

this link will help you in understanding MERGE

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

Upvotes: 2

Related Questions