Mongoose: Schema vs Model?

后端 未结 1 1984
春和景丽
春和景丽 2020-12-23 21:47

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

相关标签:
1条回答
  • 2020-12-23 21:59

    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".

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题