Remove specific item from array

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

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

eg. suppose i have node like

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

I need a query like (pseudocode):

 MATCH (n) REMOVE "oranges" IN n.some_array 
+6
source share
3 answers

Cypher does not 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"); 
+14
source

Sometimes we can get an error in the above request as “Exactly one instruction is expected per request, but received: 2” to remove this, we can also use as

 match (t:test) with t,FILTER(x IN t.some_array WHERE x <> "ORANGES") as filterdList set t.array=filterdList return t 
+2
source

The FILTER function is deprecated:

https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-filter

Instead, they consider using the [predicate of the variable IN list WHERE]. You just need to erase the filter () in brackets:

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

worked fine in my case

0
source

All Articles