【问题标题】:Mongoose Setting up Schema with Multiple Sub-Schemas using MERN StackMongoose 使用 MERN 堆栈设置具有多个子模式的模式
【发布时间】:2020-12-21 21:55:28
【问题描述】:

要求:

我正在尝试创建一个测验 Web 应用程序,用户应该能够添加多个测验。每个测验可以有多个问题,每个问题有 3-5 个答案。我目前配置它的方式是使用子模式,但是,因为这是我第一次使用 MERN 堆栈和猫鼬,我不确定如何填充和检索信息或知道我是否正确地完成了模式。

这是架构:

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;

这是我期望从 GET 请求中得到的 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"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    是的!看起来不错!但是index 字段应该是string 而不是number

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2021-02-12
      • 2020-10-17
      • 1970-01-01
      • 2019-06-12
      • 1970-01-01
      • 2013-04-07
      • 2012-07-13
      • 1970-01-01
      相关资源
      最近更新 更多