要求:
我正在尝试创建测验Web应用程序,用户应该能够添加多个测验。每个测验可以有多个问题,每个问题有3-5个答案。我目前将其定义为子模式的方式,因为这是我第一次使用MERN堆栈和Mongoose,所以我不确定如何填充和检索信息,或者不确定我是否正确完成了该模式。
这是架构:
const mongoose = require("mongoose");
const { Schema } = require("mongoose");
const optionSubSchema = new Schema({
value: {
type: String,
},
index: {
type: String,
enum: ["A", "B", "C", "D", "E"],
},
});
const answersSubSchema = new Schema({
Option1: [optionSubSchema],
Option2: [optionSubSchema],
Option3: [optionSubSchema],
Option4: [optionSubSchema],
Option5: [optionSubSchema],
});
const questionsSubSchema = new Schema({
value: {
type: String,
},
Index: {
type: Number,
},
answers: [answersSubSchema],
});
const quizSchema = new Schema({
name: {
type: String,
required: true,
},
description: {
type: String,
},
questions: [questionsSubSchema],
});
//exported with name 'quiz' for other modules to see it.
const Quiz = mongoose.model("quiz", quizSchema);
module.exports = Quiz;
这是我从获取请求中期望的MOCKUP JSON响应:
{
"Quiz1": [
{
"Question1": [
{
"Value": "Whats smallest number",
"Index": "1",
"Answers": [
{
"Option1": [
{
"Index": "A",
"Value": "17"
}
],
"Option2": [
{
"Index": "B",
"Value": "27"
}
],
"Option3": [
{
"Index": "C",
"Value": "38"
}
],
"Option4": [
{
"Index": "D",
"Value": "2"
}
],
"Option5": [
{
"Index": "E",
"Value": "99"
}
]
}
]
}
],
"Question2": [
{
"Value": "Whats the biggest number",
"Index": "1",
"Answers": [
{
"Option1": [
{
"Index": "A",
"Value": "17"
}
],
"Option2": [
{
"Index": "B",
"Value": "27"
}
],
"Option3": [
{
"Index": "C",
"Value": "38"
}
],
"Option4": [
{
"Index": "D",
"Value": "2"
}
],
"Option5": [
{
"Index": "E",
"Value": "99"
}
]
}
]
}
]
}
],
"Quiz2": [
{
"Question1": [
{
"Value": "How old am i!",
"Index": "1",
"Answers": [
{
"Option1": [
{
"Index": "A",
"Value": "17"
}
],
"Option2": [
{
"Index": "B",
"Value": "27"
}
],
"Option3": [
{
"Index": "C",
"Value": "38"
}
],
"Option4": [
{
"Index": "D",
"Value": "2"
}
],
"Option5": [
{
"Index": "E",
"Value": "99"
}
]
}
]
}
],
"Question2": [
{
"Value": "How tall am i!",
"Index": "1",
"Answers": [
{
"Option1": [
{
"Index": "A",
"Value": "17"
}
],
"Option2": [
{
"Index": "B",
"Value": "27"
}
],
"Option3": [
{
"Index": "C",
"Value": "38"
}
],
"Option4": [
{
"Index": "D",
"Value": "2"
}
],
"Option5": [
{
"Index": "E",
"Value": "99"
}
]
}
]
}
]
}
]
}
答案 0 :(得分:0)
是的!看起来还可以!但是index
字段应该是string
而不是number