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

查看:385
本文介绍了如何在Neo4j/Cypher中返回复合对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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:

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

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