模拟问答mongoDB 中的一个站点
我需要模拟问答像 mongoDB 中的 stackoverflow 这样的网站,这是我的主要问题。
我有 2 个主要对象 - 问题和用户(只需忘记其他对象,例如答案等......)。
用户提问。我需要始终向用户显示问题。可以搜索问题并浏览所有问题。更安全的说法是,用户提出的问题少于 1000 个。
用户拥有经常更新的声誉,并且当前的声誉应始终与问题一起显示。
解决方案 1:将用户嵌入到问题中 -
无需执行联接,但当用户声誉发生变化时,所有相关问题都应更新。并且不容易显示单个用户
解决方案 2:将用户和问题建模为单独的集合(就像在 RDBMS 中一样) -
现在更新声誉不是问题,但需要在用户和问题之间进行连接检索问题的时间。就像在 RDBMS 中一样。此外,mongoDB 没有联接,联接实际上是 2 个调用 - 1 个用于获取问题,另外 1 个用于获取用户,因此,如果要检索 100 个问题,则将有 100 个调用来获取 100 个单独的用户 - 不好。
解决方案 3:将用户嵌入到问题中,并为用户提供单独的集合
更新时,更新用户集合和嵌入的用户,仅显示用户时 - 使用用户
那么我应该使用哪一个?或者最好使用像 MySQL 这样的 RDBMS 来解决这个问题? mongoDB 中的字段更新速度有多快?
我很喜欢使用 mongo,因为它能够快速且轻松地将读取请求路由到副本和分片(如果我的网站是从单个服务器发展而来的,这无论如何都不太可能;( )
I need to model a Q & A site like stackoverflow in mongoDB, here are my main issues.
I have 2 main objects - Questions and users (just forget the others like answers etc...).
Users ask questions. I need to display the user with the question always. It is possible to search questions and browse all the questions. It safer to say that a user will ask less than 1000 questions.
Users have a reputation which is updated frequently and the current reputation should be shown with the question all the time.
Solution 1 : Embed the users inside the questions -
no need to perform a join but when the users reputation changes, all the relevant questions should be updated. And not easy to display single users
Solution 2 : Model users and question as separate collection (just like in RDBMS) -
now updating the reputation is not an issue BUT need to do a join between users and question every time a question is retrieved. Just like in RDBMS. Furthermore mongoDB doesn't have joins and a join is actually 2 calls - 1 to get the question and another 1 to get the user, so if there are 100 questions to retrieve there'll be 100 calls to get 100 separate users - NOT GOOD.
Soluion 3 : Embed the users inside the questions and have a separate collection for users too
When updating, update the user collection and the embedded users, when displaying only the user - use users
So which one should I use ? or else is this best solved with a RDBMS like MySQL ?. And how fast is the field updates in mongoDB ?
I'd love to use mongo because of its speed and easiness to route read requests to replicas and sharding (IF my site grew out of a single server which is unlikely anyway ;( )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案 1 对我来说像是不完整的,因为无论如何你都应该拥有所有用户的存储空间。
解决方案 2 也可以是一个解决方案,它可能比任何 RDBMS 更好,因为在文档数据库中,您无法进行“真正的”连接,这意味着您可以轻松扩展系统。
如果您的系统不像 SO 那么大,只需选择解决方案#2。
解决方案3 您无需在问题中嵌入所有用户信息,只需嵌入您需要显示的信息即可。
因此,高可扩展性系统的最佳解决方案是:
用户
- 所有用户相关信息的主要存储,此外,您还可以在此处拥有问题数、声誉、答案数以及您需要的任何统计数据问题{ShortUserInfo {UserName, Reputation, GoldBadgetsCount, ...} }
- 存储带有用户相关信息的问题,您需要显示特定问题的
答案
,也应嵌入问题中。并且可能还会包含ShortUserInfo
(左右)。解决方案 #3 使您能够轻松扩展系统并使其速度超快。但是,当用户更新他的个人资料(或声誉)时,您应该在每个问题/答案中更新此信息,但您可以异步执行此工作,在这种情况下,信息可能会过时一段时间,但没关系。您可以更改您的 SO 个人资料,并发现您的用户名在某些问题/答案中可能仍然是旧的。
希望这会对您有所帮助。
Solution 1 sonunds for me like incompleted because in any way you should have storage of all users.
Solution 2 Also can be a solution, and it's will be probably better than any rdbms because in document database you can't make a 'real' join and that's means that you can easy scale your system.
In case if you system will not so big like SO, just choose Solution #2.
Solution 3 You no need embedd within question all user info, just embed information that you need to display.
So best solution for high scalable system will be:
Users
- primary store of all user related info, in addition here you can have questions count, reputation, answers count and any statistic data you needQuestions {ShortUserInfo {UserName, Reputation, GoldBadgetsCount, ...} }
- store for questions with user related info that you need displayAnswers
to particular question should be also embedded within question. And probably will also contaisShortUserInfo
(or so).Solution #3 give you ability easy scale system and make it super fast. But when user update his profile(or reputation) you should update this information within each question/answer, but you can do this work async, in this case information can be stale for some time, but it's okay. You can change your SO profile and see that your user name can be still old on some questions/answers.
Hope this will help you.
有不同的方法可以做到这一点,但我可能会
- 用户集合
- 问题集合。在问题文档中,我会嵌入一组答案对象。
有架构设计演示文稿和视频,您可能会发现它们很有用。
There are different ways to do this but i would probably have
- a users collection
- a questions collection. in a question document i would embed an array of answer objects
There are schema design presentations and videos you might find those useful.