模拟问答mongoDB 中的一个站点

发布于 2024-11-18 09:32:11 字数 776 浏览 1 评论 0原文

我需要模拟问答像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如痴如狂 2024-11-25 09:32:11

解决方案 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 need

Questions {ShortUserInfo {UserName, Reputation, GoldBadgetsCount, ...} } - store for questions with user related info that you need display

Answers to particular question should be also embedded within question. And probably will also contais ShortUserInfo(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.

土豪我们做朋友吧 2024-11-25 09:32:11

有不同的方法可以做到这一点,但我可能会
- 用户集合
- 问题集合。在问题文档中,我会嵌入一组答案对象。

有架构设计演示文稿和视频,您可能会发现它们很有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文