从数组中删除特定元素

reo*_*eoh 6 neo4j cypher

我想使用Cypher从节点上的数组属性中删除元素.

我知道要删除的元素的值,但不知道它的索引.

例如,假设我有一个类似的节点

({some_array: ["apples", "oranges"]})
Run Code Online (Sandbox Code Playgroud)

我想要一个像(伪代码)的查询:

MATCH (n)
REMOVE "oranges" IN n.some_array
Run Code Online (Sandbox Code Playgroud)

cyb*_*sam 14

Cypher支架没有功能变异数组,但你可以创建一个新的数组"oranges"使用删除FILTER:

MATCH (n)
WHERE HAS(n.some_array)
SET n.array = FILTER(x IN n.some_array WHERE x <> "oranges");
Run Code Online (Sandbox Code Playgroud)


Chr*_*yer 6

过滤器功能被弃用:

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

他们考虑使用 [variable IN list WHERE predicate] 代替。你只需要用括号删除 filter() :

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

在我的情况下完美地工作

更新:

感谢匿名者指出“HAS()”被替换为“EXISTS()”