用于多选择问题和答案的MongoDB模式设计

时间:2018-09-07 04:54:08

标签: node.js mongodb mongoose orm

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

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

-由于每个问题集为12,因此每位考生对每项测试的答案都必须记录为满分12分。

2 个答案:

答案 0 :(得分:2)

我为每个必需的详细信息创建了猫鼬模式。您可以从中寻求帮助。我对您的要求进行了分析,并为许多模式(第一个问题模式)添加了模型,并导出为模型

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);

在这里,您会注意到,我还在计算候选人的totalAttempt以及他在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提供的mongoose之前的钩子。

答案 1 :(得分:0)

以下是您可以使用的基本语法:

首先,您需要像这样的猫鼬: var mongoose = require('mongoose');

然后,您需要像这样创建模式:

var studentSchema = mongoose.Schema({     名称:字符串,     电子邮件:字符串, });

最后一步是创建一个像这样的模型: var student = mongoose.model('student',studentSchema);

仅此而已: 这是基本结构。 :)