我想使用cypher从Neo4j返回一个复合对象,以整理我的查询.
I'd like to return a composite object from Neo4j using cypher to tidy up my queries.
举个例子,我有一个用户帐户对象,该对象具有存储为关系的权限.权限是复杂的对象,因此不能嵌套,它们现在通过关系[:HAS_PERMISSION]链接.我想做的是返回具有嵌套权限的完整复杂对象,例如下面的示例JSON对象
To give an example, I have a user account object that has permissions stored as relationships. The permissions are complex objects so can't be nested, they are now linked by the relationship [:HAS_PERMISSION]. What i'd like to do is return the full complex object with the permissions already nested, like in the example JSON object below
e.g. permissions: { action:'delete', resource:'blog posts' } { action:'edit', resource:'users' } core user account: { username:'Dave', email:'dave@test' } What i'd like: { username:'Dave', email:'dave@test' permissions: [{action:'delete', resource:'blog posts'},{action:'edit', resource:'users'}] }我当前拥有的查询:
MATCH(user:UserAccount)-[:HasPermission]->(permission:Permission) WITH {user:user, permissions:collect(permission)} AS UserAccount RETURN UserAccount问题是这并不能完全返回我想要的内容,它会返回以下内容:
The problem is this doesn't quite return what i'm after, it returns this:
{ user: { username:'Dave', email:'dave@test' }, permissions: [{action:'delete', resource:'blog posts'},{action:'edit', resource:'users'}] }请注意:我真的很想将权限列表添加到我返回的现有用户对象中.我想知道如何避免不得不在新对象上显式声明我需要的所有属性(如果可能的话).
Please note: I'd really like to add the permissions list to the existing user object i'm returning please. I'd like to know how to save me having to explicitly declare all of the properties I need on the new object (if it's possible).
推荐答案您可以根据需要设计对象:
You can design the objects as you need:
MATCH(user:UserAccount)-[:HasPermission]->(permission:Permission) WITH { username:user.username, email: user.email, permissions:collect(permission) } AS UserAccount RETURN UserAccount更新
您可以使用 apoc.map.setKey:
You can use apoc.map.setKey:
MATCH(user:UserAccount)-[:HasPermission]->(permission:Permission) WITH user, collect(permission) as permissions CALL apoc.map.setKey( user, 'permissions', permissions ) YIELD value as UserAccount RETURN UserAccount更多推荐
如何在Neo4j/Cypher中返回复合对象
发布评论