Search code examples
neo4jcypher

Delete specific element from array


I want to delete an element from an array property on a node using Cypher.

I know the value of the element I want to delete, but not its index.

e.g. suppose I have a node like

({some_array: ["apples", "oranges"]})

I want a query like (pseudocode):

MATCH (n)
REMOVE "oranges" IN n.some_array

Solution

  • Cypher doesn't have functions for mutating arrays, but you can create a new array with "oranges" removed using FILTER:

    MATCH (n)
    WHERE HAS(n.some_array)
    SET n.array = FILTER(x IN n.some_array WHERE x <> "oranges");