首页 > 解决方案 > 如何在 NEO4 Cypher 中创建属性数组?

问题描述

我正在尝试创建一个具有包含项目数组/列表的属性的实体。我不明白我该怎么做:

  1. 创建属性数组
  2. 对数组执行追加
  3. 解析数组属性

任何线索将不胜感激。

这有助于解决我面临的多对一关系问题。

标签: arraysneo4jcypher

解决方案


// Create a node with an array prop
CREATE (n:Test { my_array:['a', 'b', 'c']}) RETURN n;
+-------------------------------------+
| n                                   |
+-------------------------------------+
| (:Test {my_array: ["a", "b", "c"]}) |
+-------------------------------------+

// append the value 'd' in the array
MATCH (n:Test) SET n.my_array=n.my_array+ 'd' RETURN n;
+------------------------------------------+
| n                                        |
+------------------------------------------+
| (:Test {my_array: ["a", "b", "c", "d"]}) |
+------------------------------------------+


// Remove the value 'b' from the array
MATCH (n:Test) SET n.my_array=filter(x IN n.my_array WHERE x <> 'b')  RETURN n;
+-------------------------------------+
| n                                   |
+-------------------------------------+
| (:Test {my_array: ["a", "c", "d"]}) |
+-------------------------------------+


// Don't forget the magic UNWIND  with arrays
MATCH (n:Test) UNWIND n.my_array AS item  RETURN item;
+------+
| item |
+------+
| "a"  |
| "c"  |
| "d"  |
+------+

推荐阅读