Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 用于多文件选择问答的MongoDB模式设计_Node.js_Mongodb_Mongoose_Orm - Fatal编程技术网

Node.js 用于多文件选择问答的MongoDB模式设计

Node.js 用于多文件选择问答的MongoDB模式设计,node.js,mongodb,mongoose,orm,Node.js,Mongodb,Mongoose,Orm,我不擅长MongoDB设计,我需要帮助设计DB。存储有答案选择的问题和候选人的答案的最佳结构是什么 -如果考生在第一次考试中失败,每个考生将得到一套12道题,他们可以再参加两次考试。因此,在每次考试中,应试者应该得到不同的问题集 -由于每个问题集为12分,每个考生在每次考试中的答案必须以12分的分数记录 以下是您可以使用的基本语法: 首先,您需要像这样要求猫鼬: var mongoose=require('mongoose') 然后您需要创建如下模式: var studentSchema=mon

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

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


-由于每个问题集为12分,每个考生在每次考试中的答案必须以12分的分数记录

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

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

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

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

最后一步是创建如下模型: var student=mongoose.model('student',studentSchema)

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

我已经为每个必需的细节创建了一个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
提供的pre
save
钩子,在保存文档和计算值以声明候选人通过或失败之前。

您是否已开始在mongoose中建模架构,或者需要从头开始?我还没有创建架构,只是创建了问题集合的存根数据。我认为问题集合将具有包含问题的问题对象,问题图片和一个有选择的数组。到现在为止就这样好了。让我为这个问题添加一个答案。感谢您的回答。如果您阅读了我的问题,我需要对模式建模的想法,因为我知道基本语法。如果您能帮助我了解这些问题,答案和结果收集应该是这样的。如果我在编写代码时遇到困难,我将发布另一个问题。我真的很欣赏你的努力,这就是我所需要的,我会解决这个问题。我是node mongo的新手,所以我在理解这一点上有点困难。步骤1:我必须用像你一样的问题和选项来收集问题是的,那么我必须为正确答案创建一个集合,包含问题id和正确选项号,对吗?是的,UJ,如果你面临困难,最好先在纸上分析。对于答案选项,请为每个问题一次添加四个选项。否,请参阅文档,你将更好地了解这一点: