neo4j cypher 嵌套收集 [英] neo4j cypher nested collect

查看:48
本文介绍了neo4j cypher 嵌套收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一个包含用户、相册和照片的相册模式:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆