用于动态问答的MongoDB模式

用于动态问答的MongoDB模式,mongodb,mongodb-query,schema-design,nosql,Mongodb,Mongodb Query,Schema Design,Nosql,我有一个动态问答的当前关系模型。我试图看看是否有可能将模式转换为MongoDB以提高性能和灵活性 我们基本上有一系列的问题和问题类型。这些问题放在一个问题集中 这些问题是按照特定的顺序提出的,但对于一些问题,根据答案的不同,下一个问题可能会有所不同 例如,如果Q1=是,则询问问题Q9,否则询问问题Q2 关于如何在没有我目前使用的各种关系TAVLE的情况下设计这样的模式,有什么想法吗 按照这种结构的思路: { "Questions" : [ {

我有一个动态问答的当前关系模型。我试图看看是否有可能将模式转换为MongoDB以提高性能和灵活性

我们基本上有一系列的问题和问题类型。这些问题放在一个问题集中

这些问题是按照特定的顺序提出的,但对于一些问题,根据答案的不同,下一个问题可能会有所不同

例如,如果Q1=是,则询问问题Q9,否则询问问题Q2


关于如何在没有我目前使用的各种关系TAVLE的情况下设计这样的模式,有什么想法吗

按照这种结构的思路:

{ 
    "Questions" : 
    [
        {
            "QuestionNumber": "Q1",
            "QuestionType" : "YESNO",
            "QuestionText" : "Are you happy today?",
            "Answers" : 
            [ 
                { 
                    "Text" : "YES", 
                    "NextQuestionIfAnswered" : "Q9" 
                }, 
                { 
                    "Text" : "No", 
                    "NextQuestionIfAnswered" : "Q2" 
                }
            ],
        },

        {
            "QuestionNumber": "Q2",
            "QuestionType" : "MULTIPLE",
            "QuestionText" : "Why aren't you happy?",
            "Answers" : 
            [ 
                { 
                    "Text" : "Dog died", 
                    "NextQuestionIfAnswered" : "" 
                }, 
                { 
                    "Text" : "I'm just generally sad", 
                    "NextQuestionIfAnswered" : "" 
                }
            ],
        },
        {
            "QuestionNumber": "Q9",
            "QuestionType" : "TEXTBOX",
            "QuestionText" : "Type why you are happy into the box below",
            "Answers" : []
        }
    ]
}
因此,您有一个问题数组,每个问题都有一个问题编号、问题类型(用于呈现决策),每个可能的答案都包括您在选择指定答案时导航到的问题编号

您还可以通过在数组中的每个“答案”上添加userAnswer属性来存储用户对此文档中每个问题的答案。但根据您的用户数量,您可能希望将其保存在单独的集合中。

我是这样设计的

const { Schema } = mongoose;

const QuestionsSchema = new Schema({
  questionId: { type: String },
  questionText: { type: String, required: true, unique: true },
  options: { type: Array, required: true },
  marks: { type: Number, required: true },
  difficultyLevel: { type: Number },
  questionType: { type: String, required: true },
  correctOptions: { type: Array, required: true },
  addedAt: { type: Date, default: Date.now }
});

module.exports = mongoose.model("questions", QuestionsSchema, "questions");
API响应

    "questionText": "Select correct option1?",
    "options": [
        {
            "option1": "1",
            "isCorrect": false
        },
        {
            "option2": "2",
            "isCorrect": true
        },
        {
            "option3": "3",
            "isCorrect": false
        },
        {
            "option3": "3",
            "isCorrect": false
        }
    ],
    "marks": 1,
    "difficultyLevel": 1,
    "correctOptions": [
        1
    ],
    "questionType": "MCQ"
}