0

I'm kinda new to firebase & react-native but I need a little help with a query I have to do for my project.

So let's say I have 2 tables

Posts:{postID(key), posterID, description}
Likes:{likeID(key), postID, likerID}

so the relationship between these two tables is on postID

Is there a way to count all likes for each Post and return top 10 with most likes? I know that this process can't be all on server side so I'm fine with any solutions really.

I think this is the query to count likes for one post:

firebase.database().ref(`/likes`)
        .orderByChild('postId').equalTo(postID)
        .once('value', snapshot => {
            const likesCount = snapshot.count;

        })
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
georgcon
  • 67
  • 11
  • if you need any sort of complex queries, I would strongly recommend not using firebase, as you'll be building workarounds all day long – Kai Jan 30 '19 at 05:22

1 Answers1

1

Is there a way to count all likes for each Post...?

Firebase has no built-in aggregation queries. You'll typically either do this client-side, or through Cloud Functions, and write the count into each post. You'd then order on that "count property" to return the posts with the most likes.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807