Cypher: minimum collection value

129 views
Skip to first unread message

Wes Freeman

unread,
Oct 4, 2012, 5:42:33 PM10/4/12
to ne...@googlegroups.com
Does anyone have a way to find the minimum value in a collection in Cypher?

This would be ideal, but min doesn't work with collections, only as an aggregation:

start n=node(0)
with [3,2,1,4,5] as coll
return min(coll);
-----------
|min(coll)|
-----------
|1        |
-----------

I couldn't figure out a way to do it without conditional expressions--hoping for a clever solution using existing Cypher commands. Seems like you can almost do it with filter. Maybe a way to sort collections based on a property would be handy for this kind of thing.

Thanks,
Wes

Peter Neubauer

unread,
Oct 5, 2012, 12:24:45 AM10/5/12
to ne...@googlegroups.com
Mmh,
would this be doable with your REDUCE somehow, using a MIN in there?

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Neo4j 1.8 GA - http://www.dzone.com/links/neo4j_18_release_fluent_graph_literacy.html
> --
>
>

Wes Freeman

unread,
Oct 5, 2012, 1:10:30 AM10/5/12
to ne...@googlegroups.com
Maybe, but I couldn't come up with a way of doing it without conditional expressions inside the reduce. 

Wes

--



Peter Neubauer

unread,
Oct 5, 2012, 8:48:22 AM10/5/12
to ne...@googlegroups.com
Yeah,
maybe that coudl be expressed in ORDER BY?


start n=node(0)
with [3,2,1,4,5] as coll
return coll order by elements(coll) asc;

or so?

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Neo4j 1.8 GA - http://www.dzone.com/links/neo4j_18_release_fluent_graph_literacy.html


> --
>
>

Michael Hunger

unread,
Oct 5, 2012, 11:59:24 PM10/5/12
to ne...@googlegroups.com
I somehow see that we duplicate a lot of functionality that is available for aggregation/order by/skip limit
in working with collections.

It would be great if we could find a way of merging the two?

Perhaps it is really time to look into subqueries? Aka cypher queries on collections?
Don't know if it makes sense or not.

> start n=node(0)
match n--m
> with collect(m) as coll
return (
start x in coll // or start x = coll
where x.foo > 5
return x.name
order by x.age desc
limit 2
)

could perhaps be simplified to when only returning x

return (
x in coll // or x = coll
where x.foo > 5
order by x.age desc
limit 2
)
> --
>
>

Reply all
Reply to author
Forward
0 new messages