0
votes

I have a simple problem, having a model like this:

(User)-[r_like:USER_LIKES_DECK]->(Deck)

Where relationship r_like has property like = TRUE | FALSE to distinguish between like and dislike.

Now I need to return like and dislike count for each deck. How to achieve this?

Dummy pseudo code:

MATCH (d:Deck)
OPTIONAL MATCH (d)<-[r:USER_LIKES_DECK]-(u:User)
WITH d, count(r) WHERE r.like = true AS likedCount, count(r) WHERE r.like = false AS dislikedCount // <- NOT WORKING
RETURN d{.*, likedCount:likedCount, dislikedCount:dislikedCount}
1

1 Answers

1
votes

This should work:

MATCH (d:Deck)
OPTIONAL MATCH (d)<-[rel:USER_LIKES_DECK]-()
WITH d, REDUCE(s = {l: 0, d: 0}, r IN COLLECT(rel) |
  CASE WHEN r.like
    THEN {l: s.l+1, d: s.d}
    ELSE {l: s.l, d: s.d+1} END) AS data
RETURN d{.*, likedCount: data.l, dislikedCount: data.d}