Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-18-2021 08:39 AM
Now I want to create multiple relationships with just one query. Of course I can do it like that ...
Solved! Go to Solution.
02-20-2021 05:27 AM
and apologies for overthinking this but rather than
unwind [1,2,3,4,5,6,7,8,9,10] ... ...
or
foreach ( x in range (1,200) .... ....
you can also do
unwind range (1,200) as x ... .... .....
and so thus my previous post of
unwind [1,2,3,4,5,6,7,8,9,10] as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})
create (p1)-[:LIVING_IN]->(a1);
can become
unwind range (1,200) as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})
create (p1)-[:LIVING_IN]->(a1);
02-18-2021 11:47 AM
depending on how large of a loop but one could
unwind [1,2,3,4,5,6,7,8,9,10] as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})
create (p1)-[:LIVING_IN]->(a1);
though in the above the first pass would evaluate to {pid: 1} and {aid: 11} , the 2nd would be {pid: 2} and {aid: 12} and isnt exactly what you want so maybe {aid: 10+loop} should be {aid:9+loop} so as to get {pid: 1} and {aid: 10} and the 2nd pass of {pid: 2} and {aid: 11} etc. Simply an example
02-18-2021 12:12 PM
Interesting, but I have round about 200 pid`s that have to be related. This could work for my other nodes with less datasets.
02-18-2021 12:50 PM
ah try
foreach ( loop in range (1,200) | match (p1:Person {pid: loop}), (a1:Address {aid:10+loop}) create (p1)-[:LIVING_IN]->(a1);
02-18-2021 04:45 PM
Your first recommendation unfortunately does not create anything:
The loop-Version does not work, because of the syntax I guess.
So I tried that with a match before the foreach, but now it wants to create an existing node. I just want the combining by a new relation 😞
02-19-2021 04:19 AM
regarding the syntax error my mistake. you need one final trailing ')' at the end of the line and thus as well as my mistake again in that you can not do a create
within a foreach
. However you can merge
and presumably accomplish the same as originally requested and
foreach ( loop in range (1,200) | merge (p1:Person {pid: loop})-[:LIVING_IN]->(a1:Address {aid:10+loop}) )
and so the above will find a
:Person node at pid=loop
:Address node at aid:10+loop
if they dont exist, create said nodes and then also create a relationship between said nodes. If the :Person and :Address nodes already/previously exist then we will not create additional nodes, and the same for the relationship as well
02-19-2021 07:44 AM
I also tried with set:
02-19-2021 09:41 PM
Try this:
unwind [1,2,3,4,5,6,7,8,9,10] as ids
merge (p:Person {pid:ids})
merge (a:Address {aid: ids + 10})
merge (p)-[:HAS_ADDRESS]->(a)
return p, a
Result:
match (p:Person)-[:HAS_ADDRESS]->(a:Address)
return collect(p.pid) as pid, collect(a.aid) as aid
Result:
02-20-2021 05:27 AM
and apologies for overthinking this but rather than
unwind [1,2,3,4,5,6,7,8,9,10] ... ...
or
foreach ( x in range (1,200) .... ....
you can also do
unwind range (1,200) as x ... .... .....
and so thus my previous post of
unwind [1,2,3,4,5,6,7,8,9,10] as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})
create (p1)-[:LIVING_IN]->(a1);
can become
unwind range (1,200) as loop
match (p1:Person {pid: loop}), (a1:Address {aid:10+loop})
create (p1)-[:LIVING_IN]->(a1);
02-20-2021 05:44 AM
Thank you, the last one helped 🙂
02-19-2021 07:07 AM
I got the same error-message with merge.
All the sessions of the conference are now available online