如何在不定义模式的情况下使用 Mongoose? [英] How do you use Mongoose without defining a schema?

查看:24
本文介绍了如何在不定义模式的情况下使用 Mongoose?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以前版本的 Mongoose(用于 node.js)中,可以选择在不定义架构的情况下使用它

In previous versions of Mongoose (for node.js) there was an option to use it without defining a schema

var collection = mongoose.noSchema(db, "User");

但在当前版本中,noSchema"功能已被删除.我的架构可能会经常更改,并且真的不适合已定义的架构,那么是否有一种新方法可以在 mongoose 中使用无架构模型?

But in the current version the "noSchema" function has been removed. My schemas are likely to change often and really don't fit in with a defined schema so is there a new way to use schema-less models in mongoose?

推荐答案

我想这就是你要找的 Mongoose Strict

选项:严格

strict 选项(默认启用)可确保添加到我们模型实例中但未在我们的架构中指定的值不会保存到数据库中.

The strict option, (enabled by default), ensures that values added to our model instance that were not specified in our schema do not get saved to the db.

注意:除非有充分的理由,否则不要设置为 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!!

这篇关于如何在不定义模式的情况下使用 Mongoose?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆