8

I'd like to return a composite object from Neo4j using cypher to tidy up my queries.

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'}]
 }

The query I currently have:

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).

Charlotte Skardon
  • 6,220
  • 2
  • 31
  • 42
David Swindells
  • 641
  • 1
  • 12
  • 28

3 Answers3

23

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

Update

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
stdob--
  • 28,222
  • 5
  • 58
  • 73
  • I see what you're doing but I was wondering if there's a way to take the existing object and all of it's properties (the one in my actual system has loads so i'd like to avoid having to explicitly specify them all). – David Swindells Jun 17 '16 at 10:13
  • That's exactly what I'm looking for, thanks to both @stdob @Anomally211! – David Swindells Jun 17 '16 at 12:30
12

Since Neo4J 3.1 you can use Map projections

MATCH (user:UserAccount)-[:HasPermission]->(permission:Permission)
WITH user, collect(permission) as permissions
RETURN user{.*, permissions: permissions}
Jacob
  • 974
  • 1
  • 12
  • 21
Leo Rodríguez
  • 169
  • 2
  • 7
0

You can use the set keyword to set a property in any object, we can use this to our advantage. However Neo4j only allows us to store primitive arrays as collections in properties and does not allow us to store a map such as

[{action:'delete', resource:'blog posts'},{action:'edit', resource:'users'}]

so we will need to simplify your permissions node model a little.

What you will end up getting is something along the lines of

{ username:'Dave', email:'dave@test.com' permissions: ['delete blog posts','edit users'] }

The advantage is that you can have everything in just one object and take the contents of the entire UserAccount node.

MATCH(user:UserAccount)-[:HasPermission]->(permission:Permissions)
WITH user AS UserAccount, collect(permission.action +" "+ permission.resource) as permissions set UserAccount.permissions=permissions
return UserAccount

TLDR: Its possible to get the data without formatting the entire object like @stodb's example. But this method cannot output complex map collections and can only output primitive arrays as collections.

Anomaly211
  • 1,053
  • 9
  • 17