如果我尝试使用不在架构中的字段保存文档,我希望猫鼬失败。
我知道strict
选项(https://mongoosejs.com/docs/guide.html#strict),但是它不能满足我的需求。 strict: true
删除字段而没有警告。并且strict: false
可以保存所有内容(从长远来看是不好的。)
var thingSchema = new Schema({..}, { /* ?? */ })
var Thing = mongoose.model('Thing', thingSchema);
var thing = new Thing({ iAmNotInTheSchema: true });
thing.save();
有没有办法提供一些选择,使thing.save();
失败并显示错误?
我想为本地开发打开此选项。这样我就可以找到拼写错误和遗忘的字段而无需进行调试
答案 0 :(得分:1)
strict选项也可以设置为“ throw”,这将导致产生错误而不是丢弃不良数据。
有一个选项传递给模式构造器,以在这种情况下引发错误。参考:https://mongoosejs.com/docs/guide.html#strict
new Schema({..}, {"strict": "throw"});