Model a Q & A site in mongoDB
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 coll开发者_Go百科ection (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 ;( )
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.
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.
精彩评论