gpt4 book ai didi

node.js - 用于多项选择题和答案的 MongoDB 模式设计

转载 作者:行者123 更新时间:2023-12-05 04:04:41 24 4
gpt4 key购买 nike

我不擅长 MongoDB 设计,我在设计数据库方面需要帮助。存储有答案选择的问题候选人的答案的最佳结构是什么?

-每位考生将获得一组 12 个问题,如果考生在第一次考试中失败,他们可以再参加 2 次考试。因此,在每次考试中,考生每次都应该得到不同的问题集。

-由于每个问题集为 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);

在保存文档和计算值以声明候选通过或失败之前,我使用了 mongoose 提供的预 save Hook 。

关于node.js - 用于多项选择题和答案的 MongoDB 模式设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52215512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com