2013-12-19 2 views
1

json 결과에 매핑하려는 중첩 된 has_many 관계가 있습니다. 이 사이퍼에 중첩 된 has_many 관계

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 

같은 것을 내가 대신 원하는이다

중첩 된 아니에요 제외하고

블로그에서 다음의 예는, 내가하고 싶은 일의 종류를 보여줍니다 I 다음과 같은 구조의 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"] 
          } 
         ] 
      } 

      ] 
} 

답변

3

시도해 볼 수 있습니까?

챕터와 일치하는 항목을 유지하면 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

+0

"수집 ({이름 : 아이. name, has_read : books}) "이전에 구문을 수집하는 것을 보지 못했습니다. 어디에서 문서화되어 있습니까? –

+0

@BobB - http://neo4j.com/docs/stable/query-aggregation.html#aggregation-collect – OpenDataAlex