简体   繁体   中英

Neo4j cypher query order by with collect

In cypher query i have multiple result which i get using collect now how i can order by collect property in cypher?

MATCH(u:User) 
WITH COLLECT({name:u.name,date:u.date}) AS userinfo 
RETURN userinfo

OR in the case there are multiple collections that have been merged

MATCH(u:User)-[r:CreatedBy]->(p:Project) 
WITH COLLECT({name:p.name,date:p.date}) AS info 
MATCH(i:Institue)-[owner:Owner]->(i:Institute) 
WITH COLLECT({instituteName:i.name,date:i.date}) AS instituteinfo,info 
WITH COLLECT(instituteinfo + info) AS alldata 
RETURN alldata

You simply need to order the user nodes by the attribute of your choice prior to collecting them. Something like this..,

MATCH(u:User)
WITH u
ORDER BY u.name
WITH COLLECT({name:u.name,date:u.date}) AS userinfo 
RETURN userinfo

Or if you were looking to combine multiple collections and produce a single ordered collection you could recombine them something like this...

MATCH(u:User)-[r:CreatedBy]->(p:Project) 
WITH COLLECT({name:p.name, date:p.date}) AS info 
MATCH(i:Institue)-[owner:Owner]->(i:Institute) 
WITH COLLECT({instituteName:i.name, date:i.date}) AS instituteinfo,info 
WITH instituteinfo + info AS alldata 
UNWIND alldata as node
WITH node
ORDER BY node.name
WITH COLLECT (DISTINCT node) as alldata
RETURN alldata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM