How to create mongoose schema dynamically?

后端 未结 1 766
-上瘾入骨i
-上瘾入骨i 2020-12-08 02:46

I have an app that works on node.js with MongoDB and mongoose. My app simply sends/deletes/edits form data and for that, I have such mongoose model:

var mong         


        
相关标签:
1条回答
  • 2020-12-08 03:23

    Apply the strict: false option to your existing schema definition by supplying it as a second parameter to the Schema constructor:

    var appFormSchema = new Schema({
        User_id : {type: String},
        LogTime : {type: String},
        feeds : [new Schema({
            Name: {type: String},
            Text : {type: String}
        }, {strict: false})
        ]
    }, {strict: false});
    
    module.exports = mongoose.model('appForm', appFormSchema);
    

    If you want to leave feeds as fully schemaless, that's where you can used Mixed:

    var appFormSchema = new Schema({
        User_id : {type: String},
        LogTime : {type: String},
        feeds : [Schema.Types.Mixed]
    }, {strict: false});
    
    module.exports = mongoose.model('appForm', appFormSchema);
    
    0 讨论(0)
提交回复
热议问题