Note: Do not set to false unless you have good reason.
var thingSchema = new Schema({..}, { strict: false });
var Thing = mongoose.model('Thing', thingSchema);
var thing = new Thing({ iAmNotInTheSchema: true });
thing.save() // iAmNotInTheSchema is now saved to the db!!
混合“任何事情都有”SchemaType,它的灵活性来自于它难以维护的权衡 . 混合可通过Schema.Types.Mixed或通过传递空对象文字来获得 . 以下是等效的:var Any = new Schema({any:{}}); var Any = new Schema({any:Schema.Types.Mixed}); 由于它是无模式类型,因此您可以将值更改为您喜欢的任何其他值,但Mongoose无法自动检测并保存这些更改 . 要“告诉”Mongoose混合类型的值已更改,请调用文档的.markModified(path)方法,将路径传递给刚刚更改的Mixed类型 . person.anything = {x:[3,4,{y:“changed”}]}; person.markModified( '东西'); person.save(); //任何东西现在都会被保存
function User(user){
this.name = user.name
, this.age = user.age
}
app.post('save/user', function(req,res,next){
var u = new User(req.body)
db('mydb.users').save(u)
res.send(200)
// that's it! You've saved a user
});
4 回答
我想这就是你在找什么Mongoose Strict
option: strict
strict选项(默认情况下启用)确保添加到模型实例中的未在我们的模式中指定的值不会保存到db .
Note: Do not set to false unless you have good reason.
实际上"Mixed"(
Schema.Types.Mixed
)模式似乎与Mongoose完全相同......它接受 schema-less , freeform JS object - 所以无论你能投入什么 . 看来你必须手动触发该对象的保存,但这似乎是一个公平的权衡 .
嘿克里斯,看看Mongous . 我和mongoose有同样的问题,因为我的Schemas现在在开发中经常变化 . Mongous允许我拥有Mongoose的简单性,同时能够松散地定义和改变我的'schemas' . 我选择简单地构建标准的JavaScript对象并将它们存储在数据库中
比Mongoose简单得多,虽然我确实相信你错过了一些很酷的中间件,比如“pre” . 我不需要任何这些 . 希望这可以帮助!!!
它不再可能 .
您可以将Mongoose与具有架构和节点驱动程序的集合或其他mongo模块一起用于那些无模式的集合 .
https://groups.google.com/forum/#!msg/mongoose-orm/Bj9KTjI0NAQ/qSojYmoDwDYJ