2

i'm suprised this question hasn't been asked before, makes me kinda worry actually about my ability to understand how things work on my own....

In object oriented programming, we firstly define the abstraction, the class, and then we instantiate many concrete objects, based on the abstraction.

Why do we need to define both a Schema and a Model with Mongoose ? Aren't these 2 steps the same thing ?

Radioreve
  • 3,173
  • 3
  • 19
  • 32

1 Answers1

0

The simple reason is that "model" and "schema" are two totally different things.

  • A "schema" defines the type constraints and other logic than can refer to how you want to "define" an object. Thee are many methods to do this including basic type rules right through to defining certain methods for validation or even dynamic field construction. A Schema is basically a extended definition of an object.

  • A "model" is something that actually "ties" to storage. In that it means there is something defined in "where" to actually store the the information. The "methods" used to interact with storage are also generally associated with a model. So you can save, or find or delete based on the model association. A "schema" can therefore be "tied" to various "models", as a model is all about a storage location.

So in the basic mongoose sense, a "schema" is not something that is necessarily tied in a "one to one" relationship to a "model". A schema can have many models and therefore the separation is made.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317