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

查看:63
本文介绍了如何在不定义架构的情况下使用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"功能已被删除.我的架构可能会经常更改,并且确实不适合已定义的架构,因此,有没有一种新方法可以在猫鼬中使用无架构的模型?

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?

推荐答案

我认为这就是您要寻找的猫鼬严格

I think this is what are you looking for 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天全站免登陆