首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多桩选择问题和答案的MongoDB模式设计

多桩选择问题和答案的MongoDB模式设计
EN

Stack Overflow用户
提问于 2018-09-07 04:54:07
回答 2查看 4.2K关注 0票数 3

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

如果考生在第一次考试中不及格,他们可以参加2次以上的考试,-Each考生将得到12道题。因此,在每一次考试中,考生都应该得到不同的问题。

每个考生在每次考试中都要记录-Answer,因为每个问题集都是12题,所以必须用12分来记录。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-07 21:10:42

我已经为每个所需的细节创建了一个猫鼬模式。你可以从它那里得到帮助。我分析了一下您的需求,并为许多模式添加了模型,第一个问题模式是作为模型导出的。

代码语言:javascript
复制
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

代码语言:javascript
复制
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来添加一组问题模式,如

代码语言:javascript
复制
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);

现在准备好问题,答案选项和集合,现在需要设计候选模式,

代码语言:javascript
复制
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模型中给出的每一组的答案。这个模型的结构类似于

代码语言:javascript
复制
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挂钩。

票数 8
EN

Stack Overflow用户

发布于 2018-09-07 05:53:27

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

首先,您需要像这样要求猫鼬: var mongoose =需要量(‘mongoose’);

然后,您需要创建如下模式:

var studentSchema = mongoose.Schema({ name: String,email: String,});

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

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

票数 -5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52215512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档