Created
April 9, 2021 20:56
-
-
Save isaacssemugenyi/2351e10b4b999ebc98cd5178fef2786d to your computer and use it in GitHub Desktop.
A basic schema to creat quiz model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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