MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

बहुविकल्पीय प्रश्नों और उत्तरों के लिए MongoDB स्कीमा डिज़ाइन

मैं प्रत्येक आवश्यक विवरण के लिए एक नेवला स्कीमा बनाया है। आप इसकी मदद ले सकते हैं। मैंने आपकी आवश्यकताओं का थोड़ा विश्लेषण किया है और कई स्कीमाओं के लिए मॉडल जोड़े हैं, पहला प्रश्न स्कीमा, एक मॉडल के रूप में निर्यात किया गया है

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 embedded एम्बेड किया है के रूप में

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

जहां मैंने पहले save . का इस्तेमाल किया है mongoose . द्वारा प्रदान किए गए हुक , दस्तावेज़ को सहेजने से पहले और उम्मीदवार को पास या असफल घोषित करने के लिए मूल्यों की गणना करने से पहले।



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. उल्का में उप सरणी दस्तावेज़ खोजें

  2. मोंगोडब स्क्लाइट से 4x धीमा, सीएसवी से 2x धीमा?

  3. नेवला findOneAndUpdate Upsert _id null?

  4. MongoDB में कंसोल कैसे साफ़ करें

  5. मैं Mongodb कनेक्शन के लिए PHP 7 में libmongoc ssl को कैसे सक्षम कर सकता हूं?