1
votes

i want to write a query in cypher in order to find how many actors have acted in all of the movies in which brock lesnar has acted. My plan is to collect all the movies in which brocklesnar has acted and then loop through each of the movies and see if there are common actors in all of the movies. I am not able to write the query for the same. can anybody give me some suggestions to how to approach this problem?

match (a:`ACTOR` {name: 'Brock Lesnar'})-[:ACTS_IN]->(m:`MOVIES`) collect(m.title) as      movies foreach(c in movies return (actor)-[:ACTS_IN]->(c))

this is what i have written so far. i know it is not right but this is where i am having the problem to code

2

2 Answers

1
votes

The Cypher query below will give you an ordered list of coactors of Brock Lesnar using your optional schema.

MATCH (actor:ACTOR { name: "Brock Lesnar" })-[:ACTS_IN]->(movie:MOVIES)
WITH collect(movie) as movies, actor
MATCH (coactors:ACTOR)-[:ACTS_IN]->()<-[:ACTS_IN]-(actor)
WHERE ALL(movie in movies WHERE (movie)<-[:ACTS_IN]-(coactors))
RETURN DISTINCT coactors.name
-1
votes

this is the query i have created. it first collects all the movie in which lesner has acted. and then all the actors are checked if they have acted in all of the movies or not

MATCH (actor:ACTOR{name:'Brock Lesnar'})-[:ACTS_IN]->(movie)
with actor,collect(movie.title) as movieCollection
WHERE ALL (movies IN movieCollection WHERE (actor)-[:ACTS_IN]->(movies)) AND NOT  actor.name = "Brock Lesnar"
return actor.name