用于多选择问题和答案的MongoDB模式设计 [英] MongoDB schema design for multpile choice questions and answers
问题描述
我不擅长MongoDB设计,并且在设计数据库时需要帮助.存储具有答案选项的问题和候选人的答案的最佳结构是什么?
I'm not skilled in MongoDB designing and I need help in designing a DB. What is the best structure for Storing Questions having answer choices and Answers from candidate?
-如果每位考生在第一次考试中不及格,他们将获得12个问题,他们可以再参加2场考试.因此,每位考生每次都应获得不同的问题集.
-Each candidate will get a set of 12 questions if the candidate fails in the first exam they can attend 2 more exams. So on each exam candidate should get different set of questions each time.
-由于每个问题集为12,因此每位考生对每项测试的答案都必须记录,满分为12分.
-Answer by each candidate for each test will have to be recorded with the score out of 12 as each question set is 12.
推荐答案
我为每个必需的详细信息创建了猫鼬模式.您可以从中寻求帮助.我对您的要求进行了分析,并为许多模式(第一个问题模式)添加了模型,并作为模型导出了
I have created a mongoose schema for each required details. You can take help from it. I have analyzed a bit your requirements and adding models for many schemas, first question schema, exported as a model
import { Schema } from 'mongoose';
import { AnswerOptionSchema } from './answer-option-schema';
const mongoose = require('mongoose');
export const QuestionSchema: Schema = new Schema({
question: {
type: String,
minlength: 10,
maxlength: 1000,
},
answerOptions: {
type: [AnswerOptionSchema],
default: undefined,
validate: {
validator: function(value: any) {
return value && value.length === 4;
},
message: 'Answer options should be 4.'
}
}
}, {
timestamps: true
});
export const Question = mongoose.model('Question', QuestionSchema);
,在QuestionSchema
中,我将AnswerOptionSchema
嵌入为
import { Schema } from 'mongoose';
export const AnswerOptionSchema: Schema = new Schema({
optionNumber: {
type: Number
},
answerBody: {
type: String,
minlength: 1,
maxlength: 200,
},
isCorrectAnswer: { // you can store the correct answer with question id in another model.
type: Boolean,
default: false
}
}, {
_id: false
});
借助这些模式,我创建了一个QuestionSetSchema
,以添加一组问题模式为
With the help of these schemas, I have created a QuestionSetSchema
to add a set of question schema as
import { Schema } from "mongoose";
import { QuestionSchema } from "./question-schema";
const mongoose = require('mongoose');
export const QuestionSetSchema: Schema = new Schema({
questionSet: {
type: [QuestionSchema],
validate: {
validator: function(value: any) {
return value.length === 12;
},
message: 'Question set must be 12.'
}
},
}, {
timestamps: true
});
export const QuestionSet = mongoose.model('QuestionSet', QuestionSetSchema);
现在准备好问题,答案选项和集合,现在需要设计候选方案
Now prepared with the question, answer options and the set, now need to design the candidate schema,
import { Schema } from "mongoose";
const mongoose = require('mongoose');
export const CandidateSchema: Schema = new Schema({
name: String,
email: String, // you can store other candidate related information here.
totalAttempt: {
type: Number,
default: 0,
validate: {
validator: function(value: number) {
return value === 3;
},
message: 'You have already done three attempts.'
}
},
candidateQuestionAnswers: {
type: [Schema.Types.ObjectId],
ref: 'CandidateQuesAnswer'
}
}, {
timestamps: true
});
export const Candidate = mongoose.model('Candidate', CandidateSchema);
在这里,您会注意到,我也在计算候选人的totalAttempt以及他在CandidateQuesAnswer
模型中给出的每个答案的答案.该模型具有类似
Here, you will notice, I am also calculating the totalAttempt of the candidate and the answers for each set given by him in the CandidateQuesAnswer
model. This model has the structure like
import { Schema } from "mongoose";
export const CandidateQuesAnswerSchema = new Schema({
candidate: {
type: Schema.Types.ObjectId,
ref: 'Candidate'
},
questionSet: {
type: Schema.Types.ObjectId,
ref: 'QuestionSet'
},
questionAnswers: {
type: [Number] // You can add answer schema
},
totalScore: {
type: Number
},
isPassed: {
type: Boolean,
default: false
}
}, {
timestamps: true
});
CandidateQuesAnswerSchema.pre('save', function updateTotalScore(next) {
// update total score of the candidate here based on the correct questionAnswers and
// questionSet.
next();
});
CandidateQuesAnswerSchema.pre('save', function updateIsPassed(next) {
// update the isPassed based on the totalScore obtained by the candidate.
next();
});
export const CandidateQuesAnswer = mongoose.model('CandidateAnswer', CandidateQuesAnswerSchema);
在保存文档并计算值以声明候选通过或失败之前,我已经使用过mongoose
提供的save
之前的钩子.
Where I have used pre save
hooks provided by mongoose
, before saving the document and calculating the values to declare the candidate pass or fail.
这篇关于用于多选择问题和答案的MongoDB模式设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!