Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB schema for dynamic questions and answers

I have a current relational model for a dynamic question and answer. I am trying to see if it is possible to convert the schema into MongoDB for performance and flexibility.

We basically have a series of questions and question types. These questions are put together in a question set.

The questions are asked in a particular order but for some depending on the answer the next question asked can vary.

For example if Q1=YES then ask question Q9 else ask question Q2

Any ideas on how to design such a schema without having the various relational tavles i currently utilize?

like image 870
FarscapePROJ Avatar asked Oct 30 '13 01:10

FarscapePROJ


1 Answers

What about something along the lines of this structure:

{ 
    "Questions" : 
    [
        {
            "QuestionNumber": "Q1",
            "QuestionType" : "YESNO",
            "QuestionText" : "Are you happy today?",
            "Answers" : 
            [ 
                { 
                    "Text" : "YES", 
                    "NextQuestionIfAnswered" : "Q9" 
                }, 
                { 
                    "Text" : "No", 
                    "NextQuestionIfAnswered" : "Q2" 
                }
            ],
        },

        {
            "QuestionNumber": "Q2",
            "QuestionType" : "MULTIPLE",
            "QuestionText" : "Why aren't you happy?",
            "Answers" : 
            [ 
                { 
                    "Text" : "Dog died", 
                    "NextQuestionIfAnswered" : "" 
                }, 
                { 
                    "Text" : "I'm just generally sad", 
                    "NextQuestionIfAnswered" : "" 
                }
            ],
        },
        {
            "QuestionNumber": "Q9",
            "QuestionType" : "TEXTBOX",
            "QuestionText" : "Type why you are happy into the box below",
            "Answers" : []
        }
    ]
}

So you have an array of questions, each with a question number, question type (used for rendering decisions), and each of the possible answers includes the question number that you navigate to when the specified answer is selected.

You could store the user's answers to each question in this document as well by adding an userAnswer property on each of the "Answers" in the array. But depending on your number of users, you may want to keep this in a separate collection.

like image 63
Ryan Weir Avatar answered Nov 01 '22 22:11

Ryan Weir