Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

I designed like this

const { Schema } = mongoose;

const QuestionsSchema = new Schema({
  questionId: { type: String },
  questionText: { type: String, required: true, unique: true },
  options: { type: Array, required: true },
  marks: { type: Number, required: true },
  difficultyLevel: { type: Number },
  questionType: { type: String, required: true },
  correctOptions: { type: Array, required: true },
  addedAt: { type: Date, default: Date.now }
});

module.exports = mongoose.model("questions", QuestionsSchema, "questions");

API response

    "questionText": "Select correct option1?",
    "options": [
        {
            "option1": "1",
            "isCorrect": false
        },
        {
            "option2": "2",
            "isCorrect": true
        },
        {
            "option3": "3",
            "isCorrect": false
        },
        {
            "option3": "3",
            "isCorrect": false
        }
    ],
    "marks": 1,
    "difficultyLevel": 1,
    "correctOptions": [
        1
    ],
    "questionType": "MCQ"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top