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"}
]
}
]
}
]
}
这似乎很接近,但我无法对其进行修改以满足我的需要:嵌套的has_many关系在密码中 (问题可能出在该示例中neo4j 2.0 Web控制台不支持json语法吗?)
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?)
推荐答案
尝试以下查询:
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等分别对其进行解析
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)}
这篇关于neo4j cypher嵌套收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!