cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to create a unique node based on another node

Hi,

I'm trying to figure out what I assume is a very simple cypher code. I have a list of companies, a list of codes and a list of departments. I use merge for companies and codes, but I would like for the departments to be unique for each code even though they repeat, my data looks like this:

Company A Code 1 Finance
Company A Code 1 Finance
Company A Code 1 Finance
Company A Code 1 Sales
Company A Code 1 HR
Company B Code 11 Finance
Company B Code 12 Sales
Company B Code 13 HR

I would like the data model to look like this:

I've tried a variation of WITH and MATCH but can't get exactly that data model:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///Output.csv' as line
MERGE (c:Company { name: toUpper(line.Company) })
MERGE (co:Code { name: toUpper(line.Code) })
MERGE (c)-[:has_code]->(co)
WITH c,co
CREATE (dep:Department { name: toUpper(line.Department) })
MERGE (dep)-[:belongs_to]->(co)

1 ACCEPTED SOLUTION

Hi arunas,

Output.csv

Company,Code,Department
Company A,Code 1,Finance
Company A,Code 1,Finance
Company A,Code 1,Finance
Company A,Code 1,Sales
Company A,Code 1,HR
Company B,Code 11,Finance
Company B,Code 12,Sales
Company B,Code 13,HR

I changed your Cypher code to look like this.

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///Output.csv' as line
MERGE (c:Company { name: toUpper(line.Company) })
MERGE (co:Code { name: toUpper(line.Code) })
MERGE (c)-[:HAS_CODE]->(co)
MERGE (dep:Department { name: toUpper(line.Department) })-[:BELONGS_TO]->(co)

As a result, the Finance of Code1 of Company is one.
Is this image the same as your data model?

View solution in original post

4 REPLIES 4

Can you describe how the output you are seeing varies from your desired result?

Hi,

When I use merge I get this

And with Create I get this:
2X_e_eb4128887f9f1608e946d3e2877074cc027bfb28.png

Basically, the way I see the code it should be something like ON CREATE project CREATE UNIQUE department.

Hi arunas,

Output.csv

Company,Code,Department
Company A,Code 1,Finance
Company A,Code 1,Finance
Company A,Code 1,Finance
Company A,Code 1,Sales
Company A,Code 1,HR
Company B,Code 11,Finance
Company B,Code 12,Sales
Company B,Code 13,HR

I changed your Cypher code to look like this.

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///Output.csv' as line
MERGE (c:Company { name: toUpper(line.Company) })
MERGE (co:Code { name: toUpper(line.Code) })
MERGE (c)-[:HAS_CODE]->(co)
MERGE (dep:Department { name: toUpper(line.Department) })-[:BELONGS_TO]->(co)

As a result, the Finance of Code1 of Company is one.
Is this image the same as your data model?

Hi Koji,

That's exactly what I wanted. Thank you!