MongoDB
 sql >> Cơ Sở Dữ Liệu >  >> NoSQL >> MongoDB

Thiết kế lược đồ MongoDB cho các câu hỏi và câu trả lời về sự lựa chọn multpile

Tôi đã tạo một lược đồ mongoose cho từng chi tiết được yêu cầu. Bạn có thể giúp đỡ từ nó. Tôi đã phân tích một chút yêu cầu của bạn và thêm mô hình cho nhiều lược đồ, lược đồ câu hỏi đầu tiên, được xuất dưới dạng mô hình

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

và ở đây trong QuestionSchema , Tôi đã nhúng một AnswerOptionSchema như

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

Với sự trợ giúp của các lược đồ này, tôi đã tạo một QuestionSetSchema để thêm một tập hợp lược đồ câu hỏi dưới dạng

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

Bây giờ đã được chuẩn bị với các tùy chọn câu hỏi, câu trả lời và tập hợp, bây giờ cần thiết kế lược đồ ứng cử viên,

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

Ở đây, bạn sẽ nhận thấy, tôi cũng đang tính tổng số Lời giải của ứng viên và câu trả lời cho từng bộ do anh ta đưa ra trong CandidateQuesAnswer người mẫu. Mô hình này có cấu trúc như

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

Nơi tôi đã sử dụng trước save móc được cung cấp bởi mongoose , trước khi lưu tài liệu và tính toán các giá trị để tuyên bố thí sinh đạt hay không đạt.



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Kiểm tra xem một trường có chứa một chuỗi hay không

  2. viết cú pháp mongoDB

  3. PyMongo:Làm thế nào để Sử dụng Tổng hợp và Lưu trữ Kết quả vào Bộ sưu tập khác?

  4. mongoose - có thể có sự phụ thuộc vòng tròn?

  5. Mongoid không có trong truy vấn