Cypher查询嵌套关系(像查询一样)

Ash*_*kes 1 neo4j cypher

我试图找出是否有Cypher查询来执行地图查询功能.给出示例数据.

                        (stack)
                      /    |   \
                     /     |    \
                (item)  (item)  (item)
                 /  \      |       / \
                /    \     |      /   \
           (node) (node) (node) (node)(node)
Run Code Online (Sandbox Code Playgroud)

到目前为止,我对匹配nodes 的查询是.

START stack=node({stack}) 
MATCH (stack)-[:Item]-(item)-[:Representation]-(representation)
RETURN representation
Run Code Online (Sandbox Code Playgroud)

(representation在这个查询中相当于node我在上图中没有足够的空间来保持书写表示).

现在正如预期的那样,此查询仅返回与所有item节点相关的表示的平面列表.例如

[ representation, representation, representation.... ]
Run Code Online (Sandbox Code Playgroud)

我真正想要的是一个查询返回的嵌套结构items及其相关representation小号如

[ 
    [ item, representations ],
    [ item, representations ],
    [ item, representations ] 
]
Run Code Online (Sandbox Code Playgroud)

确切的结构return并不重要.这样我就可以轻松地映射item到它representations而不需要为每个items 发送查询representations.

这可能看起来像一个微不足道的问题,但在查看Cypher备忘单并观看视频和谷歌搜索Cypher教程之后.我还没有找到关于如何使用Cypher执行此类查询(或实际上大多数查询)的简单解释.

(另外,如果有人也知道"Cypher for dummies"教程,我会非常感激.)

And*_*res 6

诀窍是使用聚合.

RETURN item, collect(representation) as representations
Run Code Online (Sandbox Code Playgroud)

聚合在没有group by这种情况下工作.

如果您使用至少一个聚合函数(count,collect,avg,min,max你在等)的return条款,那么所有的非聚集列被视为分组密钥.

在SQL中你会写如下:

SELECT item, collect(representation) as representations
FROM ...
GROUP BY item
Run Code Online (Sandbox Code Playgroud)

这只是一个重复的声明.