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
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With