简体   繁体   English

neo4j cypher嵌套收集

[英]neo4j cypher nested collect

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?) 这似乎很接近,但我无法修改它以满足我的需要:cypher中的嵌套has_many关系 (问题是neo4j 2.0 Web控制台不支持该示例中的json语法吗?)

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 要获取节点的所有属性,可以使用节点的字符串表示形式,然后使用java等单独解析它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM