Alex Q
Alex Q

Reputation: 57

Return nodes in shortestPath

Given following database schema:

movie-db schema

I would like to return the movie that connects the actor Bruce Wilis to the director Oliver Stone using the shortestPath() function.

My attempt:

match p = shortestPath((d:Director{director_name:'Oliver Stone'})-[*]- (a:Actor{actor_name:'Bruce Willis'})) 
with nodes(p) as n 
where type(n) = 'Movie' 
return n

Upvotes: 0

Views: 48

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

The problem in your attempted query is that nodes(p) is a collection of nodes. Trying to use type() on a collection of nodes won't work. Also, type() is only used to get the type of a relationship. Nodes don't have types, they have labels.

Also, you don't know if there is just a single movie node, or multiple movie nodes, in the shortest path between your starting nodes, so the best approach is to filter the collection so you only keep movie nodes in the path:

match p = shortestPath((d:Director{director_name:'Oliver Stone'})-[*]- (a:Actor{actor_name:'Bruce Willis'})) 
return [n in nodes(p) where n:Movie] as movies

This uses list comprehension to perform the filtering of the collection. Alternately, you can use the filter() function:

return filter(n in nodes(p) where n:Movie) as movies

Upvotes: 1

Related Questions