[英]Firebase query help for react-native
I'm kinda new to firebase & react-native but I need a little help with a query I have to do for my project. 我是Firebase和react-native的新手,但我需要为项目做一些查询的帮助。
So let's say I have 2 tables 假设我有2张桌子
Posts:{postID(key), posterID, description}
Likes:{likeID(key), postID, likerID}
so the relationship between these two tables is on postID 所以这两个表之间的关系在postID上
Is there a way to count all likes for each Post and return top 10 with most likes? 有没有一种方法可以计算每个帖子的所有喜欢数,并以最多的喜欢数返回前10名? 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;
})
Is there a way to count all likes for each Post...?
有没有一种方法可以计算每个帖子的所有喜欢...?
Firebase has no built-in aggregation queries. Firebase没有内置的聚合查询。 You'll typically either do this client-side, or through Cloud Functions, and write the count into each post.
通常,您将在客户端或通过Cloud Functions进行操作,并将计数写入每个帖子中。 You'd then order on that "count property" to return the posts with the most likes.
然后,您可以根据该“计数属性”进行排序,以返回最喜欢的帖子。
Also see: 另请参阅:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.