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.

Load csv without duplicates

I have been looking all over the community a post that could solve me this probelm.

I have one table that contains personal information as follows:
Name;age;direction;
Marc,22;London;
Mike;33;Paris;
Adam;45;Madrid;
Laure;34;Londo (etc etc)

LOAD CSV WITH HEADERS FROM 'file:///persons.csv' AS per FIELDTERMINATOR ';'
CREATE (p:PERSONS { Name:per.name, Age:per.age, Dir:per.directions})

Once I load this table, I create the label PERSONS.

Then I want to create another label called just DIRECTIONS that have all the different locations of all my persons: i.e.: London, Madrid, Paris, Berlin...

I want to create this new label by selecting from the same table as before just the columns directions. My problem is that I can't code not to select duplicates, so I am a little bit stuck.

LOAD CSV WITH HEADERS FROM 'file:///persons.csv' AS per FIELDTERMINATOR ';'
CREATE (d:DIRECTIONS { Dir:per.directions})

Thanks for the help!!!

1 ACCEPTED SOLUTION

niclasko
Node Clone

Hello,

In this case it would be better to use MERGE instead of CREATE. In the following way:

LOAD CSV WITH HEADERS FROM 'file:///persons.csv' AS per FIELDTERMINATOR ';'
MERGE (d:DIRECTIONS { Dir:per.directions})

View solution in original post

3 REPLIES 3

niclasko
Node Clone

Hello,

In this case it would be better to use MERGE instead of CREATE. In the following way:

LOAD CSV WITH HEADERS FROM 'file:///persons.csv' AS per FIELDTERMINATOR ';'
MERGE (d:DIRECTIONS { Dir:per.directions})

It works!! Thanks a lot Niclas.

But I do not really understand why I have to use merge instead of create.
In fact, I still find difficult to distinguish between create, merge and match. I hope that in a couple of days I will understandthe differences.

@joseluisjordanagomez
MATCH is used to search for a described pattern (Read)
CREATE is used to write a pattern, without checking if the described pattern exists (Write)
MERGE is a combination of MATCH and CREATE - first it checks if a pattern exists and then writes (Read & Write)

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