在Neo4j收集成员订购

时间:2015-02-10 13:42:15

标签: neo4j cypher

我有以下查询从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"
  ^

如何在将电影放入收藏之前按照电影发行年订购该系列?

1 个答案:

答案 0 :(得分:2)

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

请注意,WITH部分需要包含稍后在查询中使用的所有变量(在这种情况下为actor,角色,电影)。

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中提供了类似的例子。