Reputation: 67
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;
})
Upvotes: 0
Views: 357
Reputation: 598740
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:
Upvotes: 1