我有一个树结构,就像一个文件夹结构,所以对于一个没有深度限制的嵌套项目,每个节点都有访问权限。
这是我的图表:
以下是我的疑问:
MATCH (a:Account {name: "bob"})-[r:VIEWER | EDITOR]->(c:Project)
MATCH (c)<-[:IS_PARENT*]-(p)
WHERE (p)<-[:VIEWER | EDITOR]-(a)
WITH TYPE(r) as relation, p, collect(distinct c) AS children
RETURN {name: p.name, Children: [c in children | {name: c.name, access:relation}]}
以下是我的研究结果:
这就是我想要的:
我的问题是,结果被分割成两个结果,而nested child
并不嵌套在cohort
中。
另一件棘手的事情是,如果我与节点没有关系,我就不想得到一个节点。
例如,这里我删除了bob
和cohort
之间的关系
所以我不能在我的结果中得到cohort
,如下所示:
这是我的数据,如果你想尝试:
MERGE (project:Project:RootProject {name: "project test"})
MERGE (child1:Project {name: "cohort"})
MERGE (child2:Project {name: "protocol"})
MERGE (child3:Project {name: "experience"})
MERGE (child4:Project {name: "nested child"})
MERGE (project)-[:IS_PARENT]->(child1)
MERGE (project)-[:IS_PARENT]->(child2)
MERGE (project)-[:IS_PARENT]->(child3)
MERGE (child1)-[:IS_PARENT]->(child4)
MERGE (bob:Account {name: "bob"})
MERGE (bob)-[:EDITOR]->(child4)
MERGE (bob)-[:EDITOR]->(child2)
MERGE (bob)-[:VIEWER]->(child3)
MERGE (bob)-[:VIEWER]->(child1)
MERGE (bob)-[:VIEWER]->(project)
我尝试了很多事情,但我从来没有得到一个好的结果。
发布于 2022-01-09 15:50:55
这是我的答案。主要是将json对象构造为父对象,然后将祖父母项目构建到根项目中,而不是将两者混合(第10行)。注意,我删除了获取查看器或编辑器的关系,因为删除它们也更快。
//Get the root project from bob
MATCH (a:Account {name: "bob"})-[r]->(root:RootProject)
WITH a, root, {name:root.name, access:type(r)} as rootProject
//Get only those projects without nested parent
MATCH (a)-[r]->(p:Project) WHERE EXISTS((p)-[:IS_PARENT]-(root))
WITH a, rootProject, p, type(r) as relations
//Get those projects with another parent (or grand parent of root project)
OPTIONAL MATCH (p)-[:IS_PARENT]->(gp:Project)<-[r]-(a)
//Collect the children and grandchildren
WITH rootProject, collect({children: {name: p.name, access:relations}, grandchild:(case when gp is null then [] else [{name:gp.name, access:type(r)}] end)}) as allChildren
RETURN {name: rootProject.name, access: rootProject.access, children: [c in allChildren]} as projects
发布于 2022-01-11 08:12:39
它可能无法满足您的期望,但对于可伸缩性而言,这又如何呢?
MATCH (a:Account {name: "bob"})-[r:VIEWER|EDITOR]->(c:Project)
MATCH path=(p)-[:IS_PARENT*]->(c)
WHERE (p)<-[:VIEWER | EDITOR]-(a)
WITH COLLECT(path) AS paths
CALL apoc.convert.toTree(paths, false, {
nodes: {Project: ['name']},
rels: {IS_PARENT: ['name']}
}) YIELD value
RETURN value
https://stackoverflow.com/questions/70607893
复制相似问题