Mar*_*Mar 16 mongoose mongodb node.js
我有一个应用程序,使用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},
}
]
});
Run Code Online (Sandbox Code Playgroud)
这工作得很好!
现在,我想在表单中添加一个函数,以便用户可以添加一个或多个字段来形成并在其中输入文本并发布.在客户端创建动态功能是没有问题的,但我知道我的mongoose.model必须正确构建.我的问题是:如何将该变量值(动态创建的表单数据名称及其文本)添加到mongoose模式?
我看到使用'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);
Run Code Online (Sandbox Code Playgroud)
有小费吗?提前致谢!
Joh*_*yHK 28
通过将该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);
Run Code Online (Sandbox Code Playgroud)
如果你想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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18464 次 |
| 最近记录: |