neo4j cypher嵌套收集

时间:2014-02-20 01:42:28

标签: neo4j cypher

想象一下带有用户,相册和照片的相册架构:

User -[owns]-> Album -[contains]-> Photo

我可以进行嵌套收集以获取嵌套在相册中的照片,以及嵌套在用户中的相册吗?我想要的结果类似于:

{ "users": [
    { "name": "roger dodger",
      "albums": [
        { "album": "album1",
          "photos": [
            {"url": "photo1.jpg"},
            {"url": "photo2.jpg"}
          ]
        }
      ]
    }
  ]
}

这似乎很接近,但我无法修改它以满足我的需求:Nested has_many relationships in cypher (问题可能是neo4j 2.0 Web控制台在该示例中不支持json语法吗?)

1 个答案:

答案 0 :(得分:23)

尝试此查询:

MATCH (a:USER)-[:owns]->(b:ALBUM)-[:CONTAINS]->(c:PHOTO)
WITH a,b,{url: c.name} as c_photos
WITH a,{album: b.name , photos: collect(c_photos)} as b_albums
WITH {name: a.name, albums: collect(b_albums)} as a_users
RETURN {users: collect(a_users)}

修改

要获取节点的所有属性,可以使用节点的字符串表示形式,然后使用java等单独解析它

MATCH (a:User)
WITH {user: str(a)} as users
RETURN {users: collect(users)}