neo4j cypher nested collect

匆匆过客 提交于 2019-12-20 10:41:32

问题


Imagine a photo album schema w/ Users, Albums, and Photos:

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

Can I do a nested collect to get Photos nested in Albums, and Albums nested in User? I'd like results similar to:

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

This seems close but I could not modify it to suit my needs: Nested has_many relationships in cypher (Could the problem be that neo4j 2.0 web console doesn't support the json syntax in that example?)


回答1:


Try this query:

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)}

Edit

To get all properties of a node you can use string representation of the node and then parse it separately using java etc

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


来源:https://stackoverflow.com/questions/21896382/neo4j-cypher-nested-collect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!