Younger Actors

Younger actors

Here is a query that returns the actors who acted in movies between the years 2000 and 2005. The query returns actors born on or after 1980:

cypher
MATCH (a:Actor)-[:ACTED_IN]->(m)
WHERE 2000 <= m.year <= 2005 AND a.born.year >= 1980
RETURN a.name AS Actor, a.born AS Born,
collect(DISTINCT m.title) AS Movies ORDER BY Actor

This query returns 413 Actor rows. Using this as a starting point, rewrite the above query to use pattern comprehension:

cypher
MATCH (a:Actor)
WHERE a.born.year >= 1980
// Add a WITH clause to create the list using pattern comprehension
// filter the result of the pattern comprehension to return only lists with elements
// return the Actor, Born, and Movies

Your result should return the same number of Actor rows which is 413.

For this query, size() filters out actors who have no movies in that timeframe which should be minimal so this is a reasonable solution.

How many total db hits are shown with your modified query that uses pattern comprehension? Note: You must profile the query and view the query plan to see the total db hists.

Once you executed, enter the number total db hits below and click Check Answer.

  • ✓ 55623

Hint

Add a WITH clause to create the list using pattern comprehension of actors who acted in movies released between 2000 and 2005.

Use a WHERE clause to test the size of the list to be > 0.

Do not specify the Movie label in the pattern - this will yield better performance.

The number of Actor rows returned should be 413.

How many total db hits are shown with your modified query that uses pattern comprehension? Note: You must profile the query and view the query plan to see the total db hists.

Once you have entered the answer, click the Try Again button below to continue.

Solution

You can run the following query to find the answer:

cypher
PROFILE
MATCH (a:Actor)
WHERE a.born.year >= 1980
// leave off the Movie label to get better performance
WITH a, [(a)-[:ACTED_IN]->(m) WHERE 2000 <= m.year <= 2005 | m.title] AS Movies
WHERE size(Movies) > 0
RETURN a.name as Actor, a.born as Born, Movies

For this query, size filter only filters out actors who have no movies in that timeframe which should e minimal

The number of Actor rows returned should be 413.

How many total db hits are shown with your modified query that uses pattern comprehension? Note: You must profile the query and view the query plan to see the total db hists.

Once you have entered the answer, click the Try Again button below to continue.

Summary

In this challenge, you wrote a query use pattern comprehension.

In the next module, you will learn about working with lists in Cypher.