Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete specific element from array

Tags:

neo4j

cypher

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
like image 742
reoh Avatar asked Aug 11 '15 23:08

reoh


2 Answers

The FILTER function is deprecated:

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

They consider using [variable IN list WHERE predicate] instead. You just have to erase the filter() by brackets:

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

Worked in my case perfectly

Update:

Thanks ANONYMUS for pointing out that "HAS()" is replaced with "EXISTS()"

like image 131
Christian Meyer Avatar answered Oct 29 '22 04:10

Christian Meyer


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");
like image 34
cybersam Avatar answered Oct 29 '22 04:10

cybersam