Reviewed Same Movies

Reviewed Same Movies

Let’s focus on the user named Catherine Trujillo.

Write a query that returns the names of other Users who reviewed the same movies.

Try using a path to perform the retrieval and then use nodes() to return the User nodes in the paths.

For example, start with this MATCH clause:

cypher
MATCH path = (u:User {name: 'Catherine Trujillo'})-[:RATED*2]-()

This will return all paths where the nodes are 2 hops away from Catherine Trujillo. That is, it will retrieve all Users who reviewed the same movies as Catherine.

Assuming User names are unique, and Catherine is not one of the Users, how many Users are returned?

How many Users rated the same movies as Catherine?

Once you executed the query, enter the value below and click Check Answer.

  • ✓ 646

Hint

After the MATCH clause that you were given to start with, use a WITH clause to return all nodes in the path.

Then UNWIND the list returned from nodes().

Only return unique User nodes and do not return the node for Catherine Trujillo

How many Users rated the same movies as Catherine?

Once you executed the query, enter the value below and click Try Again.

Solution

You can run the following query to find the answer:

cypher
// here is a solution for answering the question
MATCH path = (u:User {name: 'Catherine Trujillo'})-[:RATED*2]-()
WITH  nodes(path) AS n
UNWIND n AS x
WITH  x WHERE x:User AND x.name <> 'Catherine Trujillo'
RETURN DISTINCT  x.name

// If you profile this query,  you will see that it does not perform as efficiently as this:

MATCH path = (u:User {name: 'Catherine Trujillo'})-[:RATED]-()-[:RATED]-(u2:User)
RETURN DISTINCT  u2.name

// Using `nodes()` on paths and unwinding may not always be the best solution

How many Users rated the same movies as Catherine?

Once you executed the query, enter the value below and click Try Again.

Summary

In this challenge, you wrote a query that returns the names of Users that reviewed the same movies.

In the next lesson, you will learn about Cypher functions to change the types of list elements.