gpt4 book ai didi

neo4j - cypher 中的嵌套 has_many 关系

转载 作者:行者123 更新时间:2023-12-05 01:47:53 25 4
gpt4 key购买 nike

我有一个嵌套的 has_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]->(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"]
}
]
}

]
}

最佳答案

你能试试吗:

如果你保持与章节的匹配,你将需要不同的collect(distinct book.title) 否则不收集。

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

哦,如果你也想在结果中包含这些章节,那么只需添加另一个:)

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

见: http://console.neo4j.org/r/kua2pi

关于neo4j - cypher 中的嵌套 has_many 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20682024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com