在以前版本的 Mongoose(用于 node.js)中,有一个选项可以在不定义架构的情况下使用它
var collection = mongoose.noSchema(db, "User");
但在当前版本中,“noSchema”功能已被删除。我的架构可能会经常更改,并且确实不适合已定义的架构,那么有没有一种新方法可以在 mongoose 中使用无架构模型?
最佳答案
我想这就是你要找的 Mongoose Strict
选项:严格
strict 选项(默认启用)确保添加到模型实例但未在我们的架构中指定的值不会保存到数据库中。
注意:除非有充分的理由,否则不要设置为 false。
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!!
https://stackoverflow.com/questions/5370846/