Srdjan Marjanovic
Srdjan Marjanovic

Reputation: 372

Order by collection member in Neo4j

I have the following query for getting an actor, roles and movies from the example movie database in Neo4j like this:

MATCH (actor:Person {name:"Meg Ryan"})-[role:ACTED_IN]->(movies)
RETURN actor, collect({roles: role.roles, movies: movies}) as movies

It all works nice, but when I want to order the collection by movies release year, I'm encountering some issues.

I tried to do it like this:

MATCH (actor:Person {name:"Meg Ryan"})-[role:ACTED_IN]->(movies)
ORDER BY movies.released DESC
RETURN actor, collect({roles: role.roles, movies: movies}) as movies

but i get this error:

Invalid input 'R': expected 'p/P' (line 2, column 2)
"ORDER BY movies.released DESC"
  ^

How can i order the collection by the Movies release year before putting it in a collection?

Upvotes: 2

Views: 418

Answers (1)

František Hartman
František Hartman

Reputation: 15086

Use WITH keyword befor ORDER BY to sort the results before collect:

Beware that the WITH part needs to contain all variables that are used later in the query (actor,role,movies in this case).

MATCH (actor:Person {name:"Meg Ryan"})-[role:ACTED_IN]->(movies)
WITH actor,role,movies 
ORDER BY movies.released DESC
RETURN actor, collect({roles: role.roles, movies: movies}) as movies

Similar example is given in the documentation.

Upvotes: 2

Related Questions