我有一个动态问答的当前关系模型。我试图看看是否有可能将架构转换为MongoDB以获得性能和灵活性。
我们基本上有一系列问题和问题类型。这些问题放在一个问题集中。
问题按特定顺序提出,但有些问题取决于答案,下一个问题可能会有所不同。
例如,如果Q1 = YES,则问问题Q9否则问问题Q2
关于如何在没有我目前使用的各种关系条件的情况下设计这样一个模式的任何想法?
答案 0 :(得分:3)
这个结构的内容如何:
{
"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" : []
}
]
}
因此,您有一系列问题,每个问题都有一个问题编号,问题类型(用于渲染决策),每个可能的答案都包含您选择指定答案时导航到的问题编号。
您可以在本文档中存储用户对每个问题的答案,也可以在数组中的每个“答案”上添加userAnswer属性。但是,根据您的用户数量,您可能希望将其保留在单独的集合中。
答案 1 :(得分:0)
我这样设计
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响应
"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"
}