【问题标题】:MongoDB schema design for multpile choice questions and answers多选题和答案的MongoDB模式设计
【发布时间】:2019-02-12 09:25:19
【问题描述】:

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

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

-由于每个问题集是 12,因此必须记录每个考生对每个测试的回答,满分 12 分。

【问题讨论】:

  • 您是否已经开始在 mongoose 中对模式进行建模或需要从头开始?
  • 我还没有创建模式,只是创建了问题集合的存根数据。我想像一个问题集合会有问题对象,其中包含问题、问题图像和一个带有选项的数组。到目前为止就是这样
  • 好吧好吧。让我为这个问题添加一个答案。

标签: node.js mongodb mongoose orm


【解决方案1】:

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

首先你需要像这样要求猫鼬: var mongoose = require('mongoose');

那么你需要像这样创建Schema:

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

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

就是这样: 这是基本结构。 :)

【讨论】:

  • 感谢您的回复。如果您阅读了我的问题,我需要对模式进行建模的想法,因为我知道基本语法。如果您能帮助我了解问题的方式,将会很有帮助,答案和结果集合应该是这样的。如果我在编码时遇到问题,我会发布另一个问题
【解决方案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);

在这里,您会注意到,我也在计算候选人的总尝试次数以及他在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 提供的 pre save 挂钩。

【讨论】:

  • 我真的很感谢你的努力,这是我所需要的,我会努力解决这个问题的。
  • 我是 node mongo 的新手,所以我很难理解这一点。第 1 步:我必须像你一样用问题和选项来创建问题集合,然后我必须创建一个正确的集合具有问题 ID 和正确选项编号的答案对吗?
  • 是UJ,如果遇到困难也最好先在纸上分析。
  • 对于答案选项,每个问题一次添加四个选项。
  • 不,请参阅文档您会对此有更好的理解:mongoosejs.com/docs/validation.html#custom-validators
猜你喜欢
  • 1970-01-01
  • 2021-05-05
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 2018-04-13
相关资源
最近更新 更多