开发者

Mongo DB schema designing problem

I m designing a mongo db schema for a site like stackoverflow. There are questions and users. Users can add questions to their favorite list and they can search for a question within the favorite list.

I have 2 collections, as Users and Questions. Problem is how to store favorites. There are 2 options

  1. Store a list of favorite question Id s with a user
  2. Store a list of user ids (of the users who added this question to their favorites), with the question.

Which approach should I take ? Remember I need to search favorites of a user too.

For an estimate of db/record sizes, assume number of questions, user开发者_开发技巧s db operations that the stackoverflow has

For more info;

This app is an asp.net mvc written in c# and hope to use Lucene.NET for search

Thanks in advance


Have a separate collection for UserFavories is better approach. Because size of the favorites is unknown at any time, and its keep on growing

       UserFavories
                -UserID (BSON Objectid)
                - id of the user who posted
                - Name of the user who posted
                - Name of the question
                - Question id
                - url to the question

We think storing Userid, Question Id is enough to find the favorites most of the time. But in non sql, its better to store the very relevant info along with the ids (avoid joins). In this case you store id & name of the user who posted the question and name,id & url of the question, so you easily display the favorites by just querying this doc alone, like this

Mongo DB schema designing problem

its not an exact way of doing this, but it ll give you an idea..


If you designing site like SO and want to achieve same performance you for sure need denormalize your data. So, i suggest to store user favorite questions id within user and store and store user id's withing question. During favorite operation you will need insert data in two places (user, question) but you will be able to retrieve quick user/question favorites back.

BTW: If you will use lucene with mongodb you will run into problems with relevance loading from mongodb.

If you need real full text search you can try RavenDB. It also great nosql database and it natively support Lucene syntax.

Edit:

When you designing site like SO keep in the mind:

  1. denormalization
  2. async request processing
  3. background jobs


If you want to display the number of favorite flags for each question, you should probably store them with the question to avoid searching through the user database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜