本文介绍了Mongoose:模式vs模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当看教程时,通常会在模式和模型之间进行划分,特别是在处理mongoose / mongodb时。
这使得移植到postgresql有点混乱,因为'模型'似乎不存在于该系统下。这两种方法有什么区别?

When looking at tutorials there is often a delineation between a schema and a model, particularly when dealing with mongoose/mongodb.This makes porting over to postgresql somewhat confusing, as 'models' don't seem to exist under that system. What is the difference the two approaches?

例如,什么是postgres / sql ORM等价于这行?

For example, what would be a postgres/sql ORM equivalent of this line?

(mongoose和express.js):

(mongoose and express.js):

var userSchema = schema.define('local', {
    username:       String,
    password:       String,
});

module.exports = mongoose.model('User', userSchema);


推荐答案

在mongoose中,模式表示特定文档,完全或仅仅是文档的一部分。它是一种表达预期属性和值以及约束和索引的方法。模型定义与数据库交互的编程接口(读,插入,更新等)。所以一个模式回答这个集合中的数据会是什么样子?模型提供了有匹配此查询的任何记录吗?或向集合中添加新文档。

In mongoose, a schema represents the structure of a particular document, either completely or just a portion of the document. It's a way to express expected properties and values as well as constraints and indexes. A model defines a programming interface for interacting with the database (read, insert, update, etc). So a schema answers "what will the data in this collection look like?" and a model provides functionality like "Are there any records matching this query?" or "Add a new document to the collection".

在直接RDBMS中,模式由DDL语句(create table,alter table等)实现,直接概念的模型,只是SQL语句,可以做高度灵活的查询(select语句)以及基本的插入,更新,删除操作。

In straight RDBMS, the schema is implemented by DDL statements (create table, alter table, etc), whereas there's no direct concept of a model, just SQL statements that can do highly flexible queries (select statements) as well as basic insert, update, delete operations.

另一种方式想SQL的性质允许您通过只选择特定字段以及从相关表中连接记录来为每个查询定义一个模型。

Another way to think of it is the nature of SQL allows you to define a "model" for each query by selecting only particular fields as well as joining records from related tables together.

在其他ORM系统如Ruby on Rails,模式是通过ActiveRecord机制定义的,模型是你的Model子类添加的额外方法,用于定义附加的业务逻辑。

In other ORM systems like Ruby on Rails, the schema is defined via ActiveRecord mechanisms and the model is the extra methods your Model subclass adds that define additional business logic.

这篇关于Mongoose:模式vs模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 05:56