Mongoose Schema vs Model?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Mongoose is a popular Object Data Modeling (ODM) library for MongoDB and Node.js. It provides a schema-based solution to model application data, enforcing structure within the documents and giving us powerful capabilities to perform validations, queries, transformations, and more. Understanding Mongoose involves familiarizing oneself with two vital aspects: the Schema and the Model. Let's dive into each of these components in detail.
Schema in Mongoose
A Schema in Mongoose defines the structure of a document, the default values, validators, and utility methods. It is a blueprint for the data structure that provides a comprehensive overview of what your MongoDB collections will look like.
Creating a Schema
A Mongoose Schema specifies the fields and types, much like a definition in a strongly typed language. Below is an example of a Schema for a collection of User documents:
Key Features of Schemas
- Type Declaration: Every field within a Schema is defined with a type.
- Validation: Mongoose provides built-in validators like
required,minlength,maxlength,match, etc. - Default Values: The
defaultoption allows setting default values for fields. - Custom Validators: You can define custom validation logic for any field.
Schema Methods & Statics
Schemas allow the definition of custom methods and static functions that add functionality directly to the document or model level.
- Instance Methods: Are applied to individual document instances.
- Static Methods: Are attached to the model itself, enabling broader operations.
Model in Mongoose
A Model is a constructor compiled from the Schema definitions. It represents a collection of documents in the database and provides an interface to interact with the database.
Creating a Model
Once a Schema is defined, a Model can be created using the mongoose.model function. Here's how you can create a model from the userSchema:
Utilizing a Model
With the Model, you can perform various database operations. Here are some common operations:
- Creating Documents:
- Querying:
- Updating:
- Deleting:
Key Points of Mongoose Models
- Inherits from Document: Every instance of a Model is a fully functional document.
- Operations on Collection: Provides CRUD operations for documents on a MongoDB collection.
- Middleware: Models support pre and post hooks, enabling logic execution during lifecycle events.
Schema vs Model - A Summarized Comparison
| Feature | Schema | Model |
| Definition | Blueprint for database documents (structure, validations). | Interface for CRUD operations, constructed from a schema. |
| Purpose | Dictates structure and provides methods/utility methods. | Handles data manipulation and interacts with the database. |
| Methods | Custom instance methods and static functions can be added. | Built with instance methods and modeling operations. |
| Creation | Defined using mongoose.Schema. | Created with mongoose.model. |
| Usage | Primarily used for creating a Model. | Used for querying, updating, and deleting documents. |
Conclusion
Understanding the distinction between Schemas and Models in Mongoose is crucial for effectively modeling MongoDB collections in a Node.js application. Schemas provide structure, default settings, and validation for documents, whereas Models serve as the primary interface for interacting with documents in the database. Recognizing their roles helps implement a robust and maintainable database layer, aligning data handling with application logic seamlessly.
Related reading
- Mongoose unique field
- Mongoose Unique values in nested array of objects
- MongooseError MongooseServerSelectionError connection monitor to 52.6.250.23727017
- Mongoose's find method with or condition does not work properly
- MongoRepository findByThisAndThat custom Query with multiple parameters
- Mongorestore don''t know what to do with file db/collection.bson, skipping
- mongorestore file X does not have .bson extension
- monk vs mongoose for Mongodb

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.