1

I am designing a schema of a forum website like StackOverflow in MongoDB. The questions collection will look something like this, the below is a question document,

{
id : 123,
questionTitle : "",
questionDesc : "",
questionAnswers : [
                    {id: "222", 
                    answer : "",
                    answerBy : "",
                    answeredAt : ""
                    answerComment : [
                           { id: "333", 
                             commentBy : "", commentAt : "", comment :""
                            votes : [
                                     {id : "444",
                                     voteBy: "", voteAt : "", vote: "up/down"}
                                    ]
                            }]
                    },
                    {...}]
}

Now the problem is with updating comment of an answer or vote of an answer. I tried searching for ways to update nested array, but no luck. I referred this question, Updating nested arrays in mongodb but the answer suggested here is to redesign the schema.

Can somebody suggest, if there is way to update nested array, so that I can update a particular comment of an answer based on the id. Or can somebody suggest any other way to design this.

P.S: I was thinking of creating separate collection for answer and question, but don't think it is a good idea.

Hope I am clear with my question. Thanks in advance. :)

3
  • 1
    The reason for a suggestion of redesign is that updating an "array within an array" is not something MongoDB can presently handle well. The limitations are within the positional $ operator in that any matched "position" can only be from the "first" array element matched. That means the "outer" array only. As an alternate to the top answer there, I would suggest that a standard relation and extra queries is acceptable in this use case. Commented Jul 4, 2015 at 11:22
  • @BlakesSeven so you mean, I should create a separate collection for answer and use question id to relate between both the collections ? Commented Jul 4, 2015 at 13:19
  • 1
    That would probably be a way to void nested problems on that level. You need to read the documentation link and understand the limitations. Then think of logical "usage patterns" where the update contraints are met. That is how you design. A true guide would be a series of blog posts and not an answer here. Just giving pointers. Commented Jul 4, 2015 at 13:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.