Dear community, I come here as a beginner to ask for questions regarding where to look for answers.
Here are the 2 questions.
I have 1 database as follow
id ; name ; surname ; friendwith
1; A ; aaaaa ; B,C,D ;
2; B; bbbbb ; C,D ;
3; D; ddddd ; null ;
how can I import a csv file with these infos so that :
- I create a node for each name, with surname as property ?
could it be ? :
LOAD CSV WITH HEADERS from 'file:///result1.csv' as row with row where row.nom is not null
MERGE (n:name {Name: row.nom , Surname :row.surname})
- for the friendship field, how to test if null and i not, for each value I create a node, ?
- for each node created, I create a relationship between each of the values within the friendwith field of my database ?
thanks a lot in advance
There are several ways to approach this. Here is one example:
load csv with headers from "file:///results1.csv" as row
merge(n:Person{name:row.name})
set n.surname = row.surname
with n, split(row.friendwith, ",") as friends
foreach(friend in friends |
merge(m:Person{name:friend})
merge(n)-[:HAS_FRIEND]->(m)
)


Thanks, that's super helpful !
New question arises :
here is the code :
LOAD CSV WITH HEADERS from 'file:///result2.csv' as row with row where row.id is not null
MERGE (n:Juggler {Name: row.nom})
SET n.Surname = row.prenom, n.Age = row.age, n.Email = row.mail
with n, split(row.aei, ";") as aeis
foreach (aei in aeis | merge (m:Juggler{Name:aei})
merge(n)-[:AEI]->(m)
)
with n, split(row.paig,";") as paigs
foreach (paig in paigs | merge (m:Juggler{Name:paig})
merge(n)-[:PAIG]->(m)
)
returns
Variable `row` not defined (line 5, column 15 (offset: 214))
"with n, split(row.paig,";") as paigs"
what's the matter ? Where does row stop being defined ?
greetings
O
that's really helpful thanks
The ‘with’ statement pass variables from one phase of the query to the next, so any variables from before a ‘with’ call are out of scope after the ‘with’ clause if they are included in the ‘with.’ As such, ‘row’ goes out if scope after the first ‘with’. Since you need ‘row’ for your second ‘with’ in the split function, add ‘row’ to the first ‘with’ clause.