Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create relationship foreach elements in array in neo4j

I have some problem with neo4j, cypher language...

I have these types of node: Movie

 {
   "overview":"An Amazon princess comes to the world of Man to become 
   the greatest of the female superheroes.",
   "actors":[
     "Gal Gadot",
     "Chris Pine",
     "Connie Nielsen",
     "Robin Wright",
     "Danny Huston"],
  "original_title":"Wonder Woman",
  "runtime":141,
  "title":"Wonder Woman"

}

Actor

{
 "birthday":"1985-04-30",
 "place_of_birth":"Rosh Ha'ayin, Israel",
 "popularity":54.444332,
 "name":"Gal Gadot"
},

I would create a relationship "ACTED_IN" between Actor and Movie, and I would do this for each actor in the array "actors".

This is the command:

MATCH (f:Movie), (a:Actors)
FOREACH (n IN f.actors | CREATE (f)-[:ACTED_IN]->(a))   

but I don't know where to put the "WHERE CONDITION"... each elements in actors array = Actors.name.

Thank you for the help.

like image 926
randy192 Avatar asked Feb 14 '26 04:02

randy192


1 Answers

You do not need a FOREACH to do it. Change your query to:

MATCH (f:Movie)
UNWIND f.actors as names
MATCH (a:Actors {name:names})
CREATE (f)-[:ACTED_IN]->(a) 

That is: MATCH all movies and use UNWIND to transform the list of names into a sequence of rows. After it, MATCH actors by name and create the relationships between the movie and the matched actors.

like image 161
Bruno Peres Avatar answered Feb 16 '26 17:02

Bruno Peres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!