当 mongodb 的好处应该是无模式时,为什么 mongoose 使用模式? [英] Why does mongoose use schema when mongodb's benefit is supposed to be that it's schema-less?

查看:20
本文介绍了当 mongodb 的好处应该是无模式时,为什么 mongoose 使用模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 mongodb 的超级新手.我正在使用 mongoose 从 node.js 访问 mongodb,并且知道如何让事情正常工作,但我想我不明白为什么它会这样工作.

I am a super newbie to mongodb. I am using mongoose to access mongodb from node.js, and know how to get things to work, but I don't think I understand why it works the way it does.

最重要的是,我不明白为什么 mongoose 有模式",而 mongodb 的突出特点之一是它没有模式.有人可以启发我吗?谢谢.

Most importantly, I don't understand why mongoose has 'schema' when one of the standout features of mongodb is that it doesn't have schema. Could someone enlighten me? thank you.

推荐答案

没有架构的数据是无用的.你从 MongoDB 得到一个文档,你用它做什么?读一些字段?您需要知道这些字段的名称、类型和含义.这是一个架构.

Data without a schema is useless. You get a document from MongoDB, what do you do with it? Read some fields? You need to know the names, types and meanings of those fields. That’s a schema.

当人们说 MongoDB 没有模式"时,他们的真正意思是它不像 SQL 数据库那样强制执行模式.MongoDB 将模式问题推到您的应用程序级别,您可以在其中更灵活地处理它们.例如,为了向您的文档添加一个新字段,您不需要对您的集合执行全有或全无的 ALTER - 可能有数百万个条目.您只需将该字段添加到您的 ODM (Mongoose) 架构中即可.

When people say that MongoDB "has no schema", they really mean that it does not enforce schema the way SQL databases do. MongoDB pushes schema concerns up to your application level, where you can handle them more flexibly. For example, in order to add a new field to your documents, you don’t need to do an all-or-nothing ALTER on your collection—potentially millions of entries. You just add that field to your ODM (Mongoose) schema and you’re done.

这篇关于当 mongodb 的好处应该是无模式时,为什么 mongoose 使用模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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