如何在NEO4 Cypher中创建属性数组?

时间:2018-08-07 02:10:56

标签: arrays neo4j cypher

我正在尝试创建一个实体,该实体的属性包含一个数组/项列表。我不知道该怎么办:

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

任何线索将不胜感激。

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

1 个答案:

答案 0 :(得分:1)

// 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"  |
+------+