SarathSprakash
SarathSprakash

Reputation: 4624

How to push values to property array in Cypher?

I have two nodes user and files with a relationship :contains, the relationship has a property idwhich is an array, represented as

(:user)-[:contains{id:[12345]}]->(:files)

However I want to populate the property array id with values 1111 and 14567 sequentially using Cypher queries, I dont find any method to push values into the array.

After inserting 1111 to property id it will be:

(:user)-[:contains{id:[12345,1111]}]->(:files)

After inserting 14567 to property id it will be:

(:user)-[:contains{id:[12345,1111,14567]}]->(:files)

I don't know how to populate values to an property array sequentially.

Upvotes: 33

Views: 21417

Answers (3)

Christian Meyer
Christian Meyer

Reputation: 1781

If one of the attribute is null (which could result in horrible errors):

SET n.id = coalesce(n.id, []) + n.additionalId

The coalesce goes through the comma seperated list (inside the round brackets) from left to right and skips the variables that are Null values. So in this case if n.id is initially Null the coalesce would take the second parameter which is the empty array [].

Upvotes: 17

Kasun Wanniarachchi
Kasun Wanniarachchi

Reputation: 544

full query for newbies

MATCH (a:Application {name:'A'})-[r:REQUEST_TO]-(d:Application {name:'B'})
WHERE ID(r) = 684
SET r.id = r.id + 'New Id'

Upvotes: 1

jjaderberg
jjaderberg

Reputation: 9952

Adding values to an array is analogous to incrementing an integer or concatenating a string and is signified the same way, in your case (let c be your [c:contains {id:[12345]}])

c.id = c.id + 1111             //  [12345,1111]
c.id = c.id + 14567            //  [12345,1111,14567]

or

c.id = c.id + [1111,14567]     //  [12345,1111,14567]

Upvotes: 55

Related Questions