MongoDB架构设计,用于多项选择题和答案
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB架构设计,用于多项选择题和答案相关的知识,希望对你有一定的参考价值。
我不熟悉MongoDB的设计,我需要设计DB的帮助。存储问题的最佳结构是什么,具有答案选择和候选人的答案?
- 如果考生在第一次考试中失败,他们可以参加另外两门考试,每个考生将获得一组12个问题。因此,每个考试候选人每次都应该得到不同的问题。
- 每个考试的答案必须以12分中的分数记录,因为每个问题集为12。
我为每个必需的细节创建了一个mongoose模式。你可以从中获得帮助。我已经分析了你的要求并为许多模式添加模型,第一个问题模式,导出为模型
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
来添加一组问题模式
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);
现在准备了问题,答案选项和集合,现在需要设计候选模式,
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);
在这里,你会注意到,我也在计算候选人的总数和他在CandidateQuesAnswer
模型中给出的每一组的答案。这个模型有类似的结构
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);
我在使用save
提供的pre mongoose
钩子之前,保存文档并计算值以声明候选通过或失败。
以下是您可以使用的基本语法:
首先你需要像这样要求猫鼬:var mongoose = require('mongoose');
然后你需要像这样创建Schema:
var studentSchema = mongoose.Schema({name:String,email:String,});
最后一步是创建一个这样的模型:var student = mongoose.model('student',studentSchema);
这就是全部:这是基本结构。 :)
以上是关于MongoDB架构设计,用于多项选择题和答案的主要内容,如果未能解决你的问题,请参考以下文章
javascript jquery移动代码,用于检查单选按钮结果,多项选择