简体   繁体   English

Cypher:我可以返回收藏集或类似收藏吗?

[英]Cypher: can I return a collection of collections, or similar?

I currently have the following cypher to return a list of Users, with the Roles they are assigned and the Application that the Role is for. 目前,我有以下密码返回用户列表,以及为其分配的角色和该角色所针对的应用程序。

MATCH (u:User)-[:HAS_ROLE]->(r:Role)-[:ROLE_OF]->(a:App)
RETURN u as User, COLLECT([r, a]) as Roles

This returns a User and a collection of their roles and apps, but the collection is simply [roleA, appA, roleB, appA, roleC, appB...]. 这将返回一个用户及其角色和应用程序的集合,但是该集合只是[roleA,appA,roleB,appA,roleC,appB ...]。

Is there any way to return something like [[roleA, appA], [roleB, appA], [roleC, appB]...] as processing this list on the assumption that it is role, app, role, app does not seem like good practice to me. 是否有任何方法可以返回[[roleA,appA],[roleB,appA],[roleC,appB] ...]来处理此列表,但前提是它似乎不是角色,应用程序,角色,应用程序对我来说是好习惯。

I can return the roles and apps as separate collections, but then I do not know which app each role is assigned to. 我可以将角色和应用程序作为单独的集合返回,但是后来我不知道每个角色分配给哪个应用程序。 The only other way I can think of doing this is to perform multiple queries, which I do not want to do. 我可以想到的唯一其他方法是执行多个查询,而这是我不想做的。

I am sure there must be a better way, maybe using WITH, but I am new to Cypher. 我确信肯定会有更好的方法,也许可以使用WITH,但是我对Cypher并不陌生。

Many thanks for your help :) 非常感谢您的帮助 :)

You query appears to be working for me. 您的查询似乎对我有用。

http://console.neo4j.org/r/4zp6uv http://console.neo4j.org/r/4zp6uv

The output is: 输出为:

+--------------------------------------------------------------------------------------------------------+
| User               | Roles                                                                             |
+--------------------------------------------------------------------------------------------------------+
| Node[5]{name:"u1"} | [[Node[4]{name:"r1"},Node[2]{name:"a1"}],[Node[3]{name:"r2"},Node[1]{name:"a2"}]] |
+--------------------------------------------------------------------------------------------------------+
1 row
14 ms

If you are still having some issues for some reason or another you could try modifying the query slightly just to break it up. 如果由于某种原因仍然遇到问题,则可以尝试稍微修改一下查询以使其分解。

MATCH (u:User)-[:HAS_ROLE]->(r:Role)-[:ROLE_OF]->(a:App)
WITH u, [r, a] as tuple
RETURN u as User, COLLECT(tuple) as Roles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM