有效地存储和检索喜欢 [英] Efficiently storing and retrieving likes

查看:92
本文介绍了有效地存储和检索喜欢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Firebase数据库中,我有帖子,然后通过身份验证的用户可以喜欢帖子。我怎样才能有效地得到一个帖子收到的喜欢的数量。我知道使用MongoDB我可以添加/删除用户的ID列表,然后使用MongoDB函数来获得它的长度,并设置等于喜欢量,但我不知道如何使用Firebase 。我也可以将其添加/删除到列表中,然后增加一个likeCount变量,但似乎会导致并发性问题,除非Firebase具有该功能。我可以调用哪些函数来最好地处理和扩展?提前致谢!

解决方案您可以执行以下两项操作:
$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆