How can i design Schema for below product using mongoose?

后端 未结 3 1921
我在风中等你
我在风中等你 2021-01-28 21:15
name: aaaa shirts
category: shirts
subcategory: [{
        type: slimline,
        model: [{
                \"type\": \"twill\",
                \"colour\": [{
                 


        
3条回答
  •  滥情空心
    2021-01-28 21:23

    @Ratan Uday Kumar's answer is right, but another but similar way is:

    var ProductSchema = new mongoose.Schema({
        category      :{type:String},
        name          :{type:String},
        description   :{type:String},
        type          :[{type:String}],
        fabric        :[{type:String}],
        size          :[{type:Number}],
        color         :[{type:String}],
        created_at    :{ type: Date },
        updated_at    :{ type: Date, default: Date.now },
        updated:        {type: Date, default: Date.now}
    }, { versionKey: false }, {strict: false});
    
    export default mongoose.model('Product', ProductSchema);
    

    {strict: false} is for future! How? Now your schema has 10 fields, if in future you want to add an object with 12 (anything more than 10), you can do it, because there is no strictness in inserting objects with these 10 fields. Even if less fields.

提交回复
热议问题