Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
09-15-2022 09:09 AM
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;
Even I tried something like this:
Solved! Go to Solution.
09-16-2022 12:20 AM
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
09-15-2022 12:57 PM
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.
09-15-2022 09:16 PM - edited 09-15-2022 09:35 PM
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.
For example Customer Name: Lisa has two Order ID's DB003 and DB008. But its showing one connected to it.
How can I connect both the Order ID's here to one node like for Lisa?
Thank you.
09-16-2022 12:20 AM
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
All the sessions of the conference are now available online