MongoDB、Mongoose 和 Node.js。为什么要声明模型?
我是面向文档的数据库的新手。我的问题是为什么每次我的应用程序启动时都必须在 MongoDB 中声明一个模型?例如:
mongoose.model('User', {
collection: 'user',
properties: [
'created',
'username',
'password',
'email'
]} //Edited: '}' was missing in original post
);
我想一次性构建所有表,并且在我的应用程序中仅提供或查询数据。我认为所有数据架构都应该通过 CLI 或特殊的管理员(比如说 mySql 的 PhpMyAdmin)来设置。
事实证明,在我的应用程序开始时,每次我都声明一个数据模型。对我来说没有意义。帮助 :)
I am new to document oriented databases. My question is why do I have to declare a model in MongoDB each and every time my app starts? For example:
mongoose.model('User', {
collection: 'user',
properties: [
'created',
'username',
'password',
'email'
]} //Edited: '}' was missing in original post
);
I want to build all tables ONCE and in my app only feed or query for data. I thought that all data architecture should be set via CLI or a special Admin (let's say PhpMyAdmin for mySql).
It turns out that in the beginning of my application each and every time I declare a data model. Doesn't make sense to me. Help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要声明一个模型,因为本质上 MongoDB 本身只存储集合,并不真正绑定到模式/模型。 Mongoose 提供了一种定义模型的方法,但有些驱动程序甚至不提供这种方法(请参阅 Candy for Ruby)。这就是拥有面向文档的数据库的全部意义。您并未真正绑定到模式,而是根据需要动态更改数据库的数据结构。
You need to declare a model because essentially MongoDB itself only stores collections and doesn't really bind to a schema/model. Mongoose provides a method to define a model but there are drivers which really don't even provide one (see Candy for Ruby). That's the whole point of having a document-orient database. You are not really binded to a schema and makes changes in the data structure of your database on the fly, as per requirement.