Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j cypher query order by with collect

Tags:

neo4j

cypher

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
like image 655
Ritesh Tiwari Avatar asked Mar 30 '17 12:03

Ritesh Tiwari


People also ask

How do you order by Cypher?

You can order by multiple properties by stating each variable in the ORDER BY clause. Cypher will sort the result by the first variable listed, and for equals values, go to the next property in the ORDER BY clause, and so on. This returns the nodes, sorted first by their age, and then by their name.

WHAT IS WITH clause in Neo4j?

The WITH clause allows query parts to be chained together, piping the results from one to be used as starting points or criteria in the next. It is important to note that WITH affects variables in scope. Any variables not included in the WITH clause are not carried over to the rest of the query.

How do you perform an aggregation in Cypher?

The Cypher query offers aggregation similar to GROUP BY offered by SQL. The aggregate function can take multiple values and can calculate the aggregated values for them. In this recipe, we will learn the common aggregation techniques, with the help of examples.

What is unwind in Neo4j?

UNWIND expands a list into a sequence of rows.


Video Answer


1 Answers

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
like image 110
Dave Bennett Avatar answered Oct 11 '22 19:10

Dave Bennett