Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Cypher - delete array property at a given index

Tags:

neo4j

cypher

How do I remove an element in a property array using Cypher?

For example, assuming user is set in a query already, I know you can use REMOVE to remove a property, e.g

REMOVE user.favouriteColours

But if this was an array, how can I remove the first element?

REMOVE user.favouriteColours[0]

or

REMOVE user.favouriteColours.0

doesn’t work and I can’t seem to see anything on Google or the docs.

Thanks

Edit:

I should mention, I know you can RETURN with user.favouriteColours[0] to get the first element, but removing doesn’t work

like image 942
Adam Carter Avatar asked Oct 19 '25 18:10

Adam Carter


1 Answers

To remove the first element of an array you can use the tail function:

MATCH (user:User {id:123})
SET user.favouriteColors = tail(user.favouriteColors)

If you need to solve this more generically by removing the n-th element of an array;

MATCH (user:User {id:123})
SET user.favouriteColors = user.favouriteColors[0..n] +
    user.favouriteColors[n+1..length(user.favouriteColors)]
like image 50
Stefan Armbruster Avatar answered Oct 22 '25 19:10

Stefan Armbruster