在 Neo4j 中按集合成员排序

Order by collection member in Neo4j

我有以下查询,用于从 Neo4j 中的示例电影数据库中获取演员、角色和电影,如下所示:

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

一切正常,但当我想按电影发行年份排序合集时,我遇到了一些问题。

我试过这样做:

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

但我收到此错误:

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

我如何才能按电影发行年份订购合集,然后再将其放入合集中?

在 ORDER BY 之前使用 WITH 关键字在 collect 之前对结果进行排序:

请注意,WITH 部分需要包含稍后在查询中使用的所有变量(在本例中为演员、角色、电影)。

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

documentation.

中给出了类似的例子