cypher ternary operator hack

178 views
Skip to first unread message

Michael Hunger

unread,
Aug 2, 2012, 4:40:22 AM8/2/12
to ne...@googlegroups.com
As this question came up on StackOverflow and cypher doesn't support ternary operators for default values (except for coalesce) yet, I want to share the "hack" I wrote.

http://stackoverflow.com/questions/11629913/ternary-operator-default-value-in-neo4j-cypher/11773740

if an employee's city is Delhi, return 5 else return 10


Unfortunately it is not supported out of the box but here is a hack to do it, using filter, head and collection literals.

The idea is to have a two element list and a filter expression that becomes true for the first element for your "true-branch" and true for the second element in the list.

see this console example: http://console.neo4j.org/r/6tig7g


start n=node(5) return head(filter( a in [5,10] : n.city = 'DELHI' OR a = 10))

Peter Neubauer

unread,
Aug 2, 2012, 4:43:12 AM8/2/12
to ne...@googlegroups.com
Very nifty Michael!

Cheers,

/peter neubauer

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

Wanna learn something new? Come to @graphconnect.
Reply all
Reply to author
Forward
0 new messages