Neo4j Nested在cypher中有许多关系
我有一个嵌套的has\u many关系,我正试图映射到一个json结果 下面的博客示例显示了我想要做的事情,只是它不是嵌套的Neo4j Nested在cypher中有许多关系,neo4j,cypher,Neo4j,Cypher,我有一个嵌套的has\u many关系,我正试图映射到一个json结果 下面的博客示例显示了我想要做的事情,只是它不是嵌套的 MATCH (a:Person { name: "Andres" })-[:FATHER_OF]->(child) RETURN {name:a.name, kids:collect(child.name)} as document 我想要的是这样的东西 MATCH (a:Person { name: "Andres" })-[:FATHER_OF]
MATCH (a:Person { name: "Andres" })-[:FATHER_OF]->(child)
RETURN
{name:a.name, kids:collect(child.name)} as document
我想要的是这样的东西
MATCH (a:Person { name: "Andres" })-[:FATHER_OF]->(child)-[:has_read]->(book)-[:has_chapter]->(chapter)
RETURN
{name:a.name, kids:collect({"name":child.name, has_read:collect(book)})} as document
在本例中,我希望返回一个json对象,其结构如下:
{
"name": "Andres"
"kids": [
{
"name":"Bob"
"has_read": [
{
"name":"Lord of the Rings",
"chapters": ["chapter1","chapter2","chapter3"]
},
{
"name":"The Hobbit",
"chapters": ["An unexpected party","Roast mutton"]
}
]
},
{
"name":"George"
"has_read": [
{
"name":"Lord of the Rings",
"chapters": ["chapter1","chapter2","chapter3"]
},
{
"name":"Silmarillion",
"chapters": ["chapter1","chapter2"]
}
]
}
]
}
你能试试吗
如果你与这一章保持一致,你就需要有明显的区别
收集(不同的图书名称)
否则不收集
MATCH (a:Person { name: "Andres" })-[:FATHER_OF]->(child),
(child)-[:has_read]->(book)-[:has_chapter]->(chapter)
WITH a,child,collect(distinct book.title) as books
RETURN
{name:a.name,
kids:collect({name:child.name,
has_read:books})} as document
哦,如果你想在结果中也包括这些章节,那么只需添加另一个:)
见:
“collect({name:child.name,has_read:books})”我以前没有见过这种collect语法。它在任何地方都有记录吗?
MATCH (a:Person { name: "Andres" })-[:FATHER_OF]->(child),
(child)-[:has_read]->(book)-[:has_chapter]->(chapter)
WITH a,child, {title: book.title, chapters: collect(chapter.title)} as book_doc
WITH a,child, collect(book_doc) as books
RETURN
{name:a.name,
kids:collect({name:child.name,
has_read:books})} as document