如何在 Neo4j/Cypher 中返回复合对象

编程入门 行业动态 更新时间:2024-10-24 13:19:17
本文介绍了如何在 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'
}

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:

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

更多推荐

[db:关键词]

本文发布于:2023-03-30 10:45:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/777211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   如何在   Neo4j   Cypher

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!