我有一个可以在MongoDB和mongoose上的node.js上运行的应用程序。我的应用程序只是发送/删除/编辑表单数据,为此,我有这样的猫鼬模型:
var mongoose = require('mongoose');
module.exports = mongoose.model('appForm', {
User_id : {type: String},
LogTime : {type: String},
feeds : [
{
Name: {type: String},
Text : {type: String},
}
]
});
一切正常!
现在,我想向表单添加一个函数,以便用户可以向表单添加一个或多个字段,并在其中输入文本并发布。在客户端上创建该动态功能没问题,但是我知道必须正确构造mongoose.model。我的问题是:如何将变量值(动态创建的表单数据名称及其文本)添加到猫鼬模式?
我看到使用strict: false
和Schema.Types.Mixed
建议。但是,我不知道...我尝试了什么:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var feedSchema = new Schema({strict:false});
module.exports = mongoose.model('appForm', feedSchema);
有小费吗?提前致谢!
通过将strict: false
选项作为第二个参数提供给Schema
构造函数,将该选项应用于您现有的模式定义:
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);
如果您希望feeds
完全不使用模式,则可以使用Mixed
:
var appFormSchema = new Schema({
User_id : {type: String},
LogTime : {type: String},
feeds : [Schema.Types.Mixed]
}, {strict: false});
module.exports = mongoose.model('appForm', appFormSchema);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句