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

编程入门 行业动态 更新时间:2024-10-23 13:31:49
本文介绍了如何在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' } 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中返回复合对象

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

发布评论

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

>www.elefans.com

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