cancel
Showing results for 
Search instead for 
Did you mean: 

How To Avoid Multiples Nodes for Same Value

mochalla
Node Clone

Thank you.
I have a question, like If I have data like this, why am I getting 8 different nodes when I import? Where as there as only 5 different customers where 3 customer have two order id's. Can I put those two order id's into properties instead of creating two different nodes for same person?

Dataset
CODE I USED:

// Create constraints
CREATE CONSTRAINT FOR (c:Customer) REQUIRE c.id IS UNIQUE;

// Create nodes
LOAD CSV WITH HEADERS FROM 'file:///Customer.csv' AS row
WITH row
MERGE (:Customer {name: row.`Customer Name`, code: row.`Customer Code`, order: row.`Order ID`})
RETURN row;
Screenshot 2022-09-15 213724.jpg

Even I tried something like this:

// Create constraints
CREATE CONSTRAINT FOR (c:Customer) REQUIRE c.code IS UNIQUE;

 

// Create nodes
LOAD CSV WITH HEADERS FROM 'file:///Customer.csv' AS row
WITH row
MERGE (:Customer {name: row.`Customer Name`, code: row.`Customer Code`, order: row.`Order ID`})
RETURN row;


And I am getting this error:
Screenshot 2022-09-15 213854.jpg
Where am I going wrong?
1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Hello @mochalla 😊

You must change your data model and create an Order node.

// Create constraints
CREATE CONSTRAINT constraint_Customer_id IF NOT EXISTS FOR (c:Customer) REQUIRE c.id IS UNIQUE;
CREATE CONSTRAINT constraint_Order_id IF NOT EXISTS FOR (o:Order) REQUIRE o.id IS UNIQUE;

// Create nodes
// Create Customer nodes
LOAD CSV WITH HEADERS FROM 'file:///Customer.csv' AS row 
WITH row 
MERGE (c:Customer {id: row.`Customer Code`}) 
SET c.name = row.`Customer Name`;

// Create Order nodes
LOAD CSV WITH HEADERS FROM 'file:///Customer.csv' AS row 
WITH row 
MERGE (:Order {id: row.`Order ID`});

// Create relationships
// Create ORDERED relationships

LOAD CSV WITH HEADERS FROM 'file:///Customer.csv' AS row 
WITH row 
MATCH (c:Customer {id: row.`Customer Code`}) 
MATCH (o:Order {id: row.`Order ID`})
MERGE (c)-[:ORDERED]->(o);

Regards,
Cobra

View solution in original post

3 REPLIES 3

glilienfield
Ninja
Ninja

Your error is in adding all the properties to the 'Merge' clause, as these are used to find the node. For example, your orderId is not the same for the same customer, so a new node for that customer gets created. Instead, only include the property/properties that uniquely identify the node in the 'Merge' clauses properties.  The remaining properties get set using a "SET" clause. The following is an example. 

 

merge(n:Customer{id: row.id})
set n.name = row.`Customer Name`, n.code = row.`Customer Code`

 

If you are tracking customers and their orders, you should have a data model that has Customer nodes and Order nodes. You related them with a relationship, which can be done during import.

I have tried with the above mentioned example of yours.
It has given me 5 nodes, but the it is not taking all Order ID's.

// Create nodes
LOAD CSV WITH HEADERS FROM 'file:///Customer.csv' AS row
WITH row
MERGE (c:Customer{code: row.`Customer Code`})
set c.name = row.`Customer Name`, c.id = row.`Order ID`
mochalla_0-1663301611485.png
Added 5 labels, created 5 nodes, set 21 properties, completed after 83 ms.
mochalla_1-1663301663939.png

For example Customer Name: Lisa has two Order ID's DB003 and DB008. But its showing one connected to it.

mochalla_2-1663301766046.png

How can I connect both the Order ID's here to one node like for Lisa?

Thank you.



Cobra
Ninja
Ninja

Hello @mochalla 😊

You must change your data model and create an Order node.

// Create constraints
CREATE CONSTRAINT constraint_Customer_id IF NOT EXISTS FOR (c:Customer) REQUIRE c.id IS UNIQUE;
CREATE CONSTRAINT constraint_Order_id IF NOT EXISTS FOR (o:Order) REQUIRE o.id IS UNIQUE;

// Create nodes
// Create Customer nodes
LOAD CSV WITH HEADERS FROM 'file:///Customer.csv' AS row 
WITH row 
MERGE (c:Customer {id: row.`Customer Code`}) 
SET c.name = row.`Customer Name`;

// Create Order nodes
LOAD CSV WITH HEADERS FROM 'file:///Customer.csv' AS row 
WITH row 
MERGE (:Order {id: row.`Order ID`});

// Create relationships
// Create ORDERED relationships

LOAD CSV WITH HEADERS FROM 'file:///Customer.csv' AS row 
WITH row 
MATCH (c:Customer {id: row.`Customer Code`}) 
MATCH (o:Order {id: row.`Order ID`})
MERGE (c)-[:ORDERED]->(o);

Regards,
Cobra