[英]Most efficient voting system in Firebase
I'm working on an app that lets users upload pictures and rate (so vote with stars 1-5) each others pictures.我正在开发一个应用程序,它允许用户上传图片并为彼此的图片评分(所以用 1-5 颗星投票)。 But I keep thinking that the database system that I'm building is not efficient enough.
但是我一直认为我正在构建的数据库系统不够高效。 I hope that there is someone who can give me advice.
我希望有人可以给我建议。
Right now I have a database table called "images".现在我有一个名为“图像”的数据库表。 It has the following fields:
它有以下字段:
Everytime a user votes the amount_of_votes gets a +1 and the rating gets calculated by (rating*old amount of votes + new vote) / new amount of votes每次用户投票时,amount_of_votes 都会获得 +1,并且评分的计算公式为(评分*旧票数 + 新票数)/新票数
But this gives me the following problems:但这给了我以下问题:
I want to build a new voting system where there is a new table called votes and it holds the user_id and the vote that the user gives.我想构建一个新的投票系统,其中有一个名为 votes 的新表,它保存 user_id 和用户提供的投票。 But that gives me the following problem:
但这给了我以下问题:
Does anyone know what the best system is that I can use?有谁知道我可以使用的最佳系统是什么? I have spend days on the internet but I haven't found the best system yet...
我在互联网上花了几天时间,但我还没有找到最好的系统......
I didn't try implementing it myself but my approach for this will be the use of a Snapshot listener .我没有尝试自己实现它,但我的方法是使用Snapshot listener 。
It can help you to have updated information in the application whenever something changes in the document.它可以帮助您在文档中发生变化时更新应用程序中的信息。 The voting system which I use is collecting 5 stars, 4 stars , 3 stars, etc, separately.
我使用的投票系统是分别收集 5 颗星、4 颗星、3 颗星等。 For example: how many 5 stars ratings a user got, how many 4 stars a user got and so on.
例如:用户获得了多少 5 星评级,用户获得了多少 4 星等。 Then, to calculate the average rating I use the following formula:
然后,为了计算平均评分,我使用以下公式:
((no_of_votes_for_5stars * 5) + (no_of_votes_for_4stars * 4) + (no_of_votes_for_3stars * 3) + (no_of_votes_for_2stars * 2) + (no_of_votes_for_1star * 1)) / (no_of_votes_for_5stars +no_of_votes_for_4stars + no_of_votes_for_3stars + no_of_votes_for_2stars + no_of_votes_for_1star )
You can save the number of stars data separately in the Firestore database and whenever someone rates a photo, you can increment the current count.您可以在 Firestore 数据库中单独保存星级数据,并且每当有人对照片进行评分时,您都可以增加当前计数。 And you can implement the above formula inside the app code so your application gets the number of stars info and calculates the average using the above formula, and then displays it.
您可以在应用程序代码中实现上述公式,以便您的应用程序获取星数信息并使用上述公式计算平均值,然后将其显示出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.