在mongodb中存储upvotes / downvote [英] storing upvotes/downvotes in mongodb

查看:116
本文介绍了在mongodb中存储upvotes / downvote的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收集了帖子用户,用户可以在每个帖子上升高/降档。将这个存储在mongodb数据库中的最佳方法是确保用户不能多次投票给给定的文档?



我想出的最简单的nosql-ish解决方案是存储在每个 Post 文档中投票的user_ids数组(或甚至数组(user_id,vote)其中投票是+1或-1,以便用户能够改变他们的投票)。从表现的角度来看,每个帖子都可以有数千张票,这是一个好主意吗?



如何真正受欢迎的网站如Reddit,顶级帖子可以有成千上万的投票?

解决方案

MongoDB文件目前限制在最多16MB,所以假设吉尔伯特的计算准确,你不会能够在 Post 文档中存储所有6百万 user_id



但是,您可以考虑将投票存储在用户文档中(即特定用户投票的post_id )。用户对600万个不同帖子投票的可能性不太可能,所以您不能快速达到大小限制。



另一种处理方式:如果您希望对特定信息有多个投票,您可能需要将 Post 文档之外的投票存储在单独的集合中,并执行其他查询, SQL方式中的多对数表:

  user_votes {user_id:ObjectId(...),post_id:ObjectId 。),vote:-1} 

并在(user_id,post_id)上创建一个复合索引。 / p>

I have a collection of Posts and Users where users can upvote/downvote each post. What would be the best way to store this in a mongodb database to make sure that users cannot vote for a given document more than once?

The simplest nosql-ish solution I came up with is storing an array of user_ids who voted inside each Post document (or even array of (user_id, vote) where vote is +1 or -1 so that users will be able to change their vote). Is it a good idea from the performance standpoint given that each post can have thousands of votes?

What about really popular websites like Reddit where top posts can have hundreds of thousands of votes?

解决方案

MongoDB documents are currently restricted to a maximum of 16MB, so assuming Gilbert's calculations are accurate, you wouldn't be able to store all 6 Million user_ids in the Post document.

However, you could consider storing the votes in the User document instead (i.e. the post_ids that the particular user voted for). It is much less likely that a user votes on 6 million different posts, so this way you won't reach the size limit as quickly.

Another way to handle this: If you expect that many votes for a particular post, you may want to store the votes outside the Post documents in a separate collection and do an additional query, similar to a many-to-many JOIN table in SQL fashion:

user_votes { user_id: ObjectId(...), post_id: ObjectId(...), vote:-1 }

and create a compound index on (user_id, post_id).

这篇关于在mongodb中存储upvotes / downvote的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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