我创建了 3 个名称属性为“a”、“b”、“c”的测试节点,并用于apoc.coll.zip()
将两个列表组合在一起:
MATCH (n:test)
WITH collect(n.name) as nodes
WITH apoc.coll.zip(nodes, range(0, size(nodes))) as pairs
RETURN pairs;
+--------------------------------+
| pairs |
+--------------------------------+
| [["a", 0], ["b", 1], ["c", 2]] |
+--------------------------------+
结果是预期的。有趣的是,当我通过在RETURN
子句中添加另一列或通过UNWIND
ing 对来修改查询时。
1.RETURN pairs,n.name;
MATCH (n:test)
WITH n, collect(n.name) as nodes
WITH n, apoc.coll.zip(nodes, range(0, size(nodes))) as pairs
RETURN pairs,n.name;
+---------------------+
| pairs | n.name |
+---------------------+
| [["a", 0]] | "a" |
| [["b", 0]] | "b" |
| [["c", 0]] | "c" |
+---------------------+
我希望结果与查询完全相同:
MATCH (n:test)
WITH n, [["a", 0], ["b", 1], ["c", 2]] as nested
RETURN nested, n.name;
+---------------------––––––––––––––––––--+
| pairs | n.name |
+---------------------––––––––––––––––––--+
| [["a", 0], ["b", 1], ["c", 2]] | "a" |
| [["a", 0], ["b", 1], ["c", 2]] | "b" |
| [["a", 0], ["b", 1], ["c", 2]] | "c" |
+---------------------––––––––––––––––––--+
2.UNWIND pairs as pair RETURN pairs
MATCH (n:test)
WITH n, collect(n.name) as nodes
WITH n, apoc.coll.zip(nodes, range(0, size(nodes))) as pairs
UNWIND pairs as pair
RETURN pairs;
+------------+
| pairs |
+------------+
| [["a", 0]] |
| [["b", 0]] |
| [["c", 0]] |
+------------+
我希望结果与没有UNWIND
子句没有什么不同:
+--------------------------------+
| pairs |
+--------------------------------+
| [["a", 0], ["b", 1], ["c", 2]] |
+--------------------------------+
3.UNWIND pairs as pair RETURN pair
MATCH (n:test)
WITH n, collect(n.name) as nodes
WITH n, apoc.coll.zip(nodes, range(0, size(nodes))) as pairs
UNWIND pairs as pair
RETURN pair;
+----------+
| pair |
+----------+
| ["a", 0] |
| ["b", 0] |
| ["c", 0] |
+----------+
UNWIND
我希望结果与简单的嵌套列表没有什么不同:
UNWIND [["a", 0], ["b", 1], ["c", 2]] as list
RETURN list;
+----------+
| list |
+----------+
| ["a", 0] |
| ["b", 1] |
| ["c", 2] |
+----------+