modifying an array property in cypher

4,771 views
Skip to first unread message

Mat Tyndall

unread,
Jun 29, 2012, 6:55:49 PM6/29/12
to ne...@googlegroups.com
So I'm trying to check to see if a value exists in an array on a node and then if it doesn't add it. 

Example: check if 'c' is present in (node {array:['a','b']}) and if not add it (node {array:['a','b','c']}).

I get the feeling with the various functions that I can iterate through the array and use regular expressions to test each value but how do I then push a new value into the array?

I'm pretty sure I could split it into 2 queries but one would be better.

I'm going to experiment in the console but if anybody has already solved this problem I'd love to see.

Mat Tyndall

unread,
Jun 29, 2012, 10:02:00 PM6/29/12
to ne...@googlegroups.com
So turns out adding to an array is easy, "SET node.array = node.array + value", however I can't figure out how to conditionally create the array property if it doesn't exist without splitting this into 2 queries.

Michael Hunger

unread,
Jun 30, 2012, 1:00:49 AM6/30/12
to ne...@googlegroups.com
Usually you should be able to do:

start node = ...
where {value} in node.array
set node.array = node.array + {value}

Please try this

Michael

Mat Tyndall

unread,
Jun 30, 2012, 6:16:37 PM6/30/12
to ne...@googlegroups.com
that works, but if the array property hasn't been created yet it throws an error.

Russell Duhon

unread,
Jun 30, 2012, 8:46:43 PM6/30/12
to ne...@googlegroups.com
Hi,

First time posting on the list, but I've been doing some similar stuff.

You can deal with the not-yet-created problem with coalesce, eg

start node = ...
set node.array = coalesce(node.array?, []) + value

(Just tried it out on a reasonably recent milestone).

I hope that helps,
Russell

Michael Hunger

unread,
Jul 1, 2012, 3:13:19 AM7/1/12
to ne...@googlegroups.com, do...@neo4j.org
Thanks Russell, that's correct, I think we should add an example for this whole use-case to the manual.

Michael

Mat Tyndall

unread,
Jul 2, 2012, 5:19:46 AM7/2/12
to ne...@googlegroups.com, do...@neo4j.org
Awesome, thanks Russel and Michael! I knew I was missing a permutation.

And to round things out, to remove a value from an array I use the following query:

SET node.array = filter(x in node.array : not(x=value))

And yeah, adding more of these sorts of queries to the examples would be great.

-Mat


On Sunday, July 1, 2012 12:13:19 AM UTC-7, Michael Hunger wrote:
Thanks Russell, that's correct, I think we should add an example for this whole use-case to the manual.

Michael
Am 01.07.2012 um 01:46 schrieb Russell Duhon:

Hi,

First time posting on the list, but I've been doing some similar stuff.

You can deal with the not-yet-created problem with coalesce, eg

start node = ...
set node.array = coalesce(node.array?, []) + value

(Just tried it out on a reasonably recent milestone).

I hope that helps,
Russell

Bharathi Raja

unread,
May 13, 2014, 11:36:13 AM5/13/14
to ne...@googlegroups.com
Hey  i used the below query it works fine for me

match (me:Person{name:"Raja"}) set me.degree=me.degree+["UG","PG"] return me


Enjoy
Reply all
Reply to author
Forward
0 new messages