如何在 Neo4j/Cypher 中返回复合对象 [英] How to return a composite object in Neo4j/Cypher

查看:28
本文介绍了如何在 Neo4j/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.com'
}

What i'd like:
 {
  username:'Dave',
  email:'dave@test.com'
  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.com'
     },
     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:

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 中返回复合对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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