Matt Bryson
Matt Bryson

Reputation: 2964

Neo4j : can you set extra data on a resource when returning it?

I have User nodes, and then a bunch of relationships to other nodes.

As well as returning the User, I need some aggregated data about some of the relationships, following, followers etc.

At the moment, I return the User and these extra properties separately, and then in my app I have to merge the extra properties into my user object.

match (user:User {id:1234})
optional match ()<-[f:Follows]-user
with user, count(f) as following
optional match ()-[f:Follows]->user'
with user, following, count(f) as followers
return user, followers, following

Which results in

user         | following | follows
----------------------------------
User {       | 10        | 30 
  id:1234,   |           |
  name:foo   |           |
}            |           |

And then I take the results, and mix in following, follows into the User object.

However, It would be far simpler, and less code if the cypher could do this for me, so it would return one 'User' object...

 user       
----------------------------------
User { 
  id:1234,
  name:foo,
  following:10,
  follows:30
}

However, I don't want these saved against the User resource, just calculated at lookup.

Any idea if this is possible? I had a look at Collect, but I couldnt see a way to append extra data to an existing resource at return.

Upvotes: 0

Views: 61

Answers (1)

Bossie
Bossie

Reputation: 3612

Cypher can return arbitrary property maps:

match (user:User {id:1234})
optional match ()<-[f:Follows]-user
with user, count(f) as following
optional match ()-[f:Follows]->user
with user, following, count(f) as followers
return { id: user.id, name: user.name, following: following, followers: followers }

I'm not sure if this is convenient if your user carries a lot of properties, however.

Upvotes: 1

Related Questions