Neo4j - 如何创建具有树结构的嵌套对象
Neo4j - How to create a nested object with a tree structure
我有一个树结构,对我的树的每个节点都有访问权限,我想在这样的嵌套对象中只获取我有权访问的节点:
{
name: "project 1",
Children: [
{name: "nested child"}
]
}
这是我的图结构:
我想获取所有与 Marion
有 :VIEWER
关系的节点,这是我试过的查询,但我没有嵌套结构
MATCH path =(a:Account {firstName: "Marion"})-[:VIEWER]->()
WITH collect(path) AS paths
CALL apoc.convert.toTree(paths)
YIELD value
RETURN value;
我得到这个结构
{
"firstName": "Marion",
"viewer": [
{
"_type": "Project",
"name": "project 1",
"_id": 1,
"type": "project"
},
{
"_type": "Child",
"name": "nested child",
"_id": 4,
"type": "child"
}
],
"_type": "Account",
"_id": 29,
}
nested child
应该在 children
对象中 project1
父->子关系的深度没有限制
有谁知道这是否可行?
如果您对我的图形结构有更好的想法,我愿意进行优化
EDIT
感谢您的回答,它有效,但我想添加一些参数
不仅可以有其他类型的关系 :VIEWER
还可以有 :VIEWER :EDITOR :OWNER
我需要知道每个节点的关系Marion
我已经进行了此查询,但我不明白为什么我的结果被拆分成表格?
MATCH (c)<-[:IS_PARENT_OF*]-(p)
WHERE (p)<-[:VIEWER | EDITOR]-(a)
// collect the child nodes per project
WITH TYPE(r) as relation, p,collect(distinct c) AS children
// return a map that contains project and children names
RETURN {name: p.name, Children: [c in children | {name: c.name, access:relation}]}
RESULT
{
"name": "project 1",
"Children": [
{
"access": "EDITOR",
"name": "Experimental design"
}
]
}
{
"name": "project 1",
"Children": [
{
"access": "VIEWER",
"name": "nested child"
}
]
}
EXPECTED
{
"name": "project 1",
"Children": [
{
"access": "EDITOR",
"name": "Experimental design"
},
{
"access": "VIEWER",
"name": "nested child"
}
]
}
根据您的节点可视化,我假设项目节点与子节点具有不同的标签。
我会尝试以下方法:
// get the top-level project
MATCH (a:Account {firstName: "Marion"})-[:VIEWER]->(p:Project)
// get the child nodes
MATCH (p)-[:IS_PARENT_OF*]->(child)
WHERE (child)<-[:VIEWER]-(a)
// collect the child nodes per project
WITH p, collect(distinct child) AS children
// return a map that contains project and children names
RETURN {name: p.name, Children: [c in children | {name: c.name}]}
编辑:
添加了 Hakan 的回答
// get the children
MATCH (a:Account {firstName: "Marion"})-[:VIEWER]->(child:Child)
// get the parent project that is also viewable
MATCH (p)-[:IS_PARENT_OF*]->(child)
WHERE (p)<-[:VIEWER]-(a)
// collect the child nodes per project
WITH p, collect(distinct child) AS children
// return a map that contains project and children names
RETURN {name: p.name, Children: [c in children | {name: c.name}]}
我有一个树结构,对我的树的每个节点都有访问权限,我想在这样的嵌套对象中只获取我有权访问的节点:
{
name: "project 1",
Children: [
{name: "nested child"}
]
}
这是我的图结构:
我想获取所有与 Marion
有 :VIEWER
关系的节点,这是我试过的查询,但我没有嵌套结构
MATCH path =(a:Account {firstName: "Marion"})-[:VIEWER]->()
WITH collect(path) AS paths
CALL apoc.convert.toTree(paths)
YIELD value
RETURN value;
我得到这个结构
{
"firstName": "Marion",
"viewer": [
{
"_type": "Project",
"name": "project 1",
"_id": 1,
"type": "project"
},
{
"_type": "Child",
"name": "nested child",
"_id": 4,
"type": "child"
}
],
"_type": "Account",
"_id": 29,
}
nested child
应该在 children
对象中 project1
父->子关系的深度没有限制
有谁知道这是否可行?
如果您对我的图形结构有更好的想法,我愿意进行优化
EDIT
感谢您的回答,它有效,但我想添加一些参数
不仅可以有其他类型的关系 :VIEWER
还可以有 :VIEWER :EDITOR :OWNER
我需要知道每个节点的关系Marion
我已经进行了此查询,但我不明白为什么我的结果被拆分成表格?
MATCH (c)<-[:IS_PARENT_OF*]-(p)
WHERE (p)<-[:VIEWER | EDITOR]-(a)
// collect the child nodes per project
WITH TYPE(r) as relation, p,collect(distinct c) AS children
// return a map that contains project and children names
RETURN {name: p.name, Children: [c in children | {name: c.name, access:relation}]}
RESULT
{
"name": "project 1",
"Children": [
{
"access": "EDITOR",
"name": "Experimental design"
}
]
}
{
"name": "project 1",
"Children": [
{
"access": "VIEWER",
"name": "nested child"
}
]
}
EXPECTED
{
"name": "project 1",
"Children": [
{
"access": "EDITOR",
"name": "Experimental design"
},
{
"access": "VIEWER",
"name": "nested child"
}
]
}
根据您的节点可视化,我假设项目节点与子节点具有不同的标签。
我会尝试以下方法:
// get the top-level project
MATCH (a:Account {firstName: "Marion"})-[:VIEWER]->(p:Project)
// get the child nodes
MATCH (p)-[:IS_PARENT_OF*]->(child)
WHERE (child)<-[:VIEWER]-(a)
// collect the child nodes per project
WITH p, collect(distinct child) AS children
// return a map that contains project and children names
RETURN {name: p.name, Children: [c in children | {name: c.name}]}
编辑:
添加了 Hakan 的回答
// get the children
MATCH (a:Account {firstName: "Marion"})-[:VIEWER]->(child:Child)
// get the parent project that is also viewable
MATCH (p)-[:IS_PARENT_OF*]->(child)
WHERE (p)<-[:VIEWER]-(a)
// collect the child nodes per project
WITH p, collect(distinct child) AS children
// return a map that contains project and children names
RETURN {name: p.name, Children: [c in children | {name: c.name}]}