Łukasz Jagodziński
Łukasz Jagodziński

Reputation: 3079

Structure for a Questions - Answers Collections

I'm not proficient in MongoDB and I have short question. What is the best structure for Questions - Answers Collections. Should I separate them to different collections or keep Answers as a property of Question? To give you more details:

  1. Each question can have many Answers
  2. I don't need to search in Answers
  3. I will search in Questions
  4. Each Answer will have userId, votes and of course answer properties
  5. After finding Question, I will be able to expand Answers list and vote for the Answer
  6. Questions will be also votable

It will be something like stackoverflow but much simpler. Thanks in advance.

Upvotes: 1

Views: 188

Answers (1)

attish
attish

Reputation: 3150

I would keep them in two separated collections. As you said you have to be able to query in the questions collection so make a questions collection, you only have to show up answers realted for a specific selected question but will have some specific fields as you mentioned like votes. If we think about stackoverflow for example, you see the questions : click on the question --> then you can query an answers collection for the answer related to that question, also for answers you can have comments which if you not expect insane much of them can kept in a list along with the answer.You can keep the answers of the questions in a collection with a reference and an index on the question id.

Something like his one:

questions:

{
    "_id" : ObjectId("52205df3d6c893638bc2aa14"),
    "q" : "What is hip?",
    "v" : 1000
}

answers :

{
    "_id" : ObjectId("52205e72d6c893638bc2aa15"),
    "q_id" : ObjectId("52205df3d6c893638bc2aa14"),
    "a" : "Tell me, tell me!",
       "u" : "uid",
    "v" : 153,
    "c" : [
        {
            "c" : "Cool",
            "u" : "uid",
            "v" : 100
        }
    ]
}

Upvotes: 1

Related Questions