有效地存储和检索喜欢 [英] Efficiently storing and retrieving likes
问题描述
$ b 1)投票节点与UID作为关键和一个值来总结所有的票。
文章:{
//所有数据
都喜欢:{
$ user_1:1,
$ user_2:-1,
}
}
然后获得一个SingleValue事件或一个Value事件(取决于是否要跟踪更改)并总结所有孩子
2)你可以使用一个交易区块,只需要保存一个值,然后根据票数来增加或减少它。
是一个链接,你可以找到安卓,iOS或Java的交易)
https://firebase.google.com/docs/database/web/save-data#save_data_as_transactions
post:{
//所有数据,
喜欢:2,
}
这取决于你想存储多少信息,以及一旦用户已经投了某个帖子,用户可以做些什么,
推荐使用两者,以保持用户喜欢(如在Facebook)的灵活性,所以他可以不同的东西,并使用数字交易,以保持其可扩展性..所以,如果一个职位得到100万喜欢,你不必数到100万喜欢每次有人加载帖子
In my Firebase database I have posts and then authenticated users can "like" posts. How can I efficiently get the number of likes a post has received. I know using MongoDB I can add/remove the user's id to a list and then use a MongoDB function to get the length of it very quickly and set that equal to the likes amount, but I'm not suer how to do that using Firebase. I could also add/remove it to the list and increment a likeCount variable, but that seems like it would cause concurrency issues unless Firebase has a function for that. What functions can I call to best handle this and scale well? Thanks in advance!
You can do both things:
1) Create a votes node with the UID as key and a value to sum up all the votes.
post:{
//All the data
likes:{
$user_1:1,
$user_2:-1,
}
}
And then just get a SingleValue Event or a Value event(depending if you want to keep track of changes) and sum up all the children
2)You can use a transaction block and just save a value and increase or decrease it depending on the votes
(here is a link where you can find transactions for android,iOS or java) https://firebase.google.com/docs/database/web/save-data#save_data_as_transactions
post:{
//All the data,
likes:2,
}
It really depends on how much information you want to store, and what the user can do once he/she already voted for some post,
I would recommend using both, to keep flexibility for the user to like (like in Facebook) so he can unlike something and use the transaction with number to keep it scalable.. so if a post gets 1,000,000 likes you don't have to count the 1,000,000 likes every time someone loads the post
这篇关于有效地存储和检索喜欢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!