Sunday, November 20, 2011

My answer to "mongodb collections choice question"

This is my answer to a Question on Support site for LampCMS project


There is no such thing as one thread here. Both Questions and Answers can contain comments. Comments are stored as nested arrays. So QUESTIONS and ANSWERS collections are very similar in structure but they are not exactly the same. For example the QUESTION has a concept of being "unanswered" and also can have the "best answer", while ANSWER does not have these properties.

Also if a question contains so many answers that it requires a pagination, it is then easier to select paginated results from dedicated ANSWERS collection rather than using extra condition to only select answers from a single collection and also paginate results on top of that.

Also sorting questions by recent, popular, unanswered, active is easier when dealing with a dedicated collection - QUESTIONS rather than adding extra condition + sorting condition. It just makes some sorting and pagination operations more efficient.

Same thing when paginating the main page (home page, for example), it's a lot easier and also more efficient when you work with just one collection - QUESTIONS, you just pass the pagination conditions to one collections and get the results.

Also in the long run when you have millions and maybe tens of millions of questions and answers it may be more easier to scale, for example you can store questions and answers on separate servers at some point.

I am sure it is possible to store all in one collection, even questions, answers and comments. But I am glad I chose the separate collections approach, I think it makes the program faster, also easier to work with and also be ready to better handle situation where a site grows to tens of millions of questions/answers.

No comments:

Post a Comment