apoc.coll.zip() 的一些奇怪的结果
Some weird results of apoc.coll.zip()
我创建了 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
对
修改查询时
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] |
+----------+
对于列出的所有 3 个查询,关键点在,
...
WITH n, collect(n.name) as nodes
...
collect
是一个 aggregating function 并且可以分组。在 WITH
子句中指定 'n' 会导致它执行类似于 SQL 分组的“分组依据”。因此,如果你有 3 个节点,你会得到 3 个结果。
您可以在 WITH
之后通过 RETURN
调试以查看每一步的结果,
像这样,
MATCH (n:test)
WITH n, collect(n.name) as nodes
RETURN n, nodes
我创建了 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
对
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] |
+----------+
对于列出的所有 3 个查询,关键点在,
...
WITH n, collect(n.name) as nodes
...
collect
是一个 aggregating function 并且可以分组。在 WITH
子句中指定 'n' 会导致它执行类似于 SQL 分组的“分组依据”。因此,如果你有 3 个节点,你会得到 3 个结果。
您可以在 WITH
之后通过 RETURN
调试以查看每一步的结果,
像这样,
MATCH (n:test)
WITH n, collect(n.name) as nodes
RETURN n, nodes