cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Get All Nodes in specific related Nodes in one Row

Hello togehter,

I started with neo4j and Cypher last week, so I am an absolute newbie (and my english is not so good as well, sorry for that). I like neo4j

I want to migrate my current project from mysql to neo4j/cypher. I created a social network with Groups, Posts, Replies and Likes. A Post can only be written in one group. A Reply has a relationship with the post. My plan is to get all Posts (who are relevant for current user) and there replies but in one query. In the same query I want to return the information if the current user liked the post, how many replies exists in total and the replies themselves (but only the latest three or something else).

My Query looks like that and Graph UI shows the right results:

MATCH (post:Post)
MATCH (me:User {email: 'my@mail.tld'})
WHERE ((post)-[:IS_WRITTEN_IN|IS_WARNING_FOR]->(:FeedGroup)<-[:IS_MEMBER_OF]-(me)) OR ((post)-[:WROTE|LIKES|REPLIED_TO]-(me))
WITH post, (()-[:BELONGS_TO]->(post)) AS replies, me
RETURN post, EXISTS((me)-[:LIKES]->(post)) AS currentUserLiked, replies, count(replies) AS totalReplies
ORDER BY toInteger(post.created) DESC
SKIP 1 LIMIT 10

This is the result and is looking right:

But my row looks not so good, because my totalReplies shows always 1 and my replies-column is my path as array. Is it possible to get only the replies from the path so that my results looks like that:

| post | replies      | currentUserLiked | totalReplies |
|------|--------------|------------------|--------------|
| p1   | [r1, r2, r3] | true             | 3            |
| p2   | []           | false            | 0            |

I tried it with OPTIONAL MATCH but than I got not all posts. I don't know what I can do right now. I googled a lot, tried a lot of things, but without a solution. That is the reason why I need your help.

Regards

2 REPLIES 2

ameyasoft
Graph Maven
As replies is an array, use size(replies) as totalReplies

Replies is an array, but of path. So it includes the post and BELONG_TO as well. But thanks for your tipp. Again what learned

EDIT:
Okay, I got it:

MATCH (post:Post)
MATCH (me:User {email: 'my@mail.tld'})
WHERE ((post)-[:IS_WRITTEN_IN|IS_WARNING_FOR]->(:FeedGroup)<-[:IS_MEMBER_OF]-(me)) OR ((post)-[:WROTE|LIKES|REPLIED_TO]-(me))
OPTIONAL MATCH ((reply:Reply)-[:BELONGS_TO]->(post))
RETURN post, EXISTS((me)-[:LIKES]->(post)) AS currentUserLiked, collect(reply), size(collect(reply)) AS totalReplies
ORDER BY toInteger(post.created) DESC
SKIP 1 LIMIT 5