如何使用对象,数组和键创建嵌套模式

时间:2019-06-21 08:01:24

标签: node.js mongoose

我目前正在尝试为MERN应用程序创建嵌套模式(我刚刚开始学习猫鼬),我感到创建的模式未正确完成。我想验证此响应:

quiz: [
    {
       question: "What is 4 X 4 ? ",
       answers: [
            { 
               type: "Smart",
               content: "16" 
            },
            {
               type: "Below_Average",
               content: "15"
            },
            { 
               type: "Clueless",
               content: "1" 
            }
        ]            
    },
   ]

//这就是我所做的

const QuizSchema = new Schema({    
    question: {
        type: String,
        default: ''
    },
    answers: {
        type: Array
    },
    type: {
        type: String,
        default: ''
    },
    content: {
        type: String,
        default: ''
    } 
});

正确的方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以创建类似这样的嵌套对象数组:

const QuizSchema = new Schema({

    question: {
        type:String,
        default:''
    },
    answers: [{
        type: {
            type: String,
            default:''
        },
        content: {
            type: String,
            default:''
        },
    }]
});