Cypher 查询 return 图作为对象,其中每个父节点包含其子节点的数组?
Cypher query to return graph as object, where each parent node contains an array of its children?
我有一张如下图所示的图表:
我必须使用什么密码查询才能return将图形作为对象,其中每个父节点都包含其子节点的数组?
示例:
state: {
name: 'New York'
publishingCompanies: [
{
name: 'Penguin'
authors: [
{
name: 'George Orwell',
books: [
{ name: 'Why I Write'},
{ name: 'Animal Farm'},
{ name: '1984' }
]
},
{
name: 'Vladimir Nobokov'
books: [
{ name: 'Lolita' }
]
},
...
]
},
{
name: 'Random House',
authors: [
...
]
}
]
}
我尝试使用 apoc.convert.toTree
,但它 return 编辑了从 State
到 Book
的路径数组。
这应该 return state
对象(假设状态名称是通过 stateName
parameter 传入的):
MATCH (s:State)
WHERE s.name = $stateName
OPTIONAL MATCH (s)-[:IS_STATE_OF]->(c)
OPTIONAL MATCH (c)-[:PUBLISHED_FOR]->(a)
OPTIONAL MATCH (a)-[:WROTE]->(b)
WITH s, c, a, CASE WHEN b IS NULL THEN [] ELSE COLLECT({name: b.name}) END AS books
WITH s, c, CASE WHEN a IS NULL THEN [] ELSE COLLECT({name: a.name, books: books}) END AS authors
WITH s, CASE WHEN c IS NULL THEN [] ELSE COLLECT({name: c.name, authors: authors}) END AS pcs
RETURN {name: s.name, publishingCompanies: pcs} AS state
我有一张如下图所示的图表:
我必须使用什么密码查询才能return将图形作为对象,其中每个父节点都包含其子节点的数组?
示例:
state: {
name: 'New York'
publishingCompanies: [
{
name: 'Penguin'
authors: [
{
name: 'George Orwell',
books: [
{ name: 'Why I Write'},
{ name: 'Animal Farm'},
{ name: '1984' }
]
},
{
name: 'Vladimir Nobokov'
books: [
{ name: 'Lolita' }
]
},
...
]
},
{
name: 'Random House',
authors: [
...
]
}
]
}
我尝试使用 apoc.convert.toTree
,但它 return 编辑了从 State
到 Book
的路径数组。
这应该 return state
对象(假设状态名称是通过 stateName
parameter 传入的):
MATCH (s:State)
WHERE s.name = $stateName
OPTIONAL MATCH (s)-[:IS_STATE_OF]->(c)
OPTIONAL MATCH (c)-[:PUBLISHED_FOR]->(a)
OPTIONAL MATCH (a)-[:WROTE]->(b)
WITH s, c, a, CASE WHEN b IS NULL THEN [] ELSE COLLECT({name: b.name}) END AS books
WITH s, c, CASE WHEN a IS NULL THEN [] ELSE COLLECT({name: a.name, books: books}) END AS authors
WITH s, CASE WHEN c IS NULL THEN [] ELSE COLLECT({name: c.name, authors: authors}) END AS pcs
RETURN {name: s.name, publishingCompanies: pcs} AS state