Skip to content

Instantly share code, notes, and snippets.

@isaacssemugenyi
Created April 9, 2021 20:56
Show Gist options
  • Save isaacssemugenyi/2351e10b4b999ebc98cd5178fef2786d to your computer and use it in GitHub Desktop.
Save isaacssemugenyi/2351e10b4b999ebc98cd5178fef2786d to your computer and use it in GitHub Desktop.
A basic schema to creat quiz model
const { Schema, model } = require('mongoose');
// Answer Schema
const answerSchema = Schema({
answer: {
type: String,
required: true
},
correct: {
type: Boolean,
required: true
}
});
// Question schema, uses the answerSchema as type for options
const questionSchema = Schema({
question: {
type: String,
required: true
},
options: [answerSchema]
});
// quizSchema takes in the questionSchema an Array of all questions
const quizSchema = Schema({
category: {
type: Schema.ObjectId,
ref: 'Category'
},
quizName: {
type: String,
required: true
},
questions: [questionSchema]
});
const Quiz = model('Quiz', quizSchema);
module.exports = Quiz;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment