0

我创建了 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子句中添加另一列或通过UNWINDing 对来修改查询时。

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] |
+----------+

你知道为什么会发生这些吗?它们似乎没有在文档中RETURN进行解释。UNWIND

4

1 回答 1

1

对于列出的所有 3 个查询,关键点是,

...
WITH n, collect(n.name) as nodes
...

collect是一个聚合函数,它可以被分组。在子句中指定“n”WITH会导致它执行类似于 SQL 分组的“分组依据”。因此,如果您有 3 个节点,则会得到 3 个结果。

RETURN您可以通过ing 之后的 ing 进行调试,WITH以查看每一步的结果,如下所示,

MATCH (n:test)
WITH n, collect(n.name) as nodes
RETURN n, nodes
于 2021-10-27T10:39:22.180 回答