Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 37 ยท Getting Started

What Is Mongoose?

Learn what Mongoose is, why Node.js developers use it on top of MongoDB, and how schemas, models and documents fit together. Start here.

Mongoose is an object data modeling (ODM) library for Node.js and MongoDB. MongoDB itself stores flexible JSON-like documents and does not force any shape on them. That freedom is great at first and painful later: a typo in a field name, a string where a number belongs, or a missing required value will be stored without a complaint. Mongoose adds a layer of structure on top of the official MongoDB driver so your application catches those mistakes early.

In this course you will learn Mongoose from the first connection to aggregation, indexes, testing and real projects. You should know basic JavaScript and have a rough idea of what a MongoDB document is: a JSON-like object with an _id.

About the examples on this site. The site cannot run a MongoDB server or install npm packages, so the real Mongoose code in this course is shown in read-only blocks. Every lesson also has runnable <TryIt> blocks that run on real Node.js 24. These use small, clearly labelled stand-ins: plain JavaScript that imitates one idea (a schema check, a query filter, a middleware chain) so you can experiment with the concept. To run the real code, install Mongoose on your own machine in the next lesson.

The three words: schema, model, document

Mongoose is built around three ideas. Learn these and the rest of the library follows.

Three core ideas

Schema Describes the shape of a document
Model A class built from a schema
Document One instance, one record

Schema defines, model talks to the database, document holds data.

1 / 2

A schema is a plain object-like description of what a document may contain: field names, types, defaults and rules. A model is a constructor compiled from a schema. It is the object you use to read and write a collection. A document is one instance of a model: it holds the data, knows how to validate itself and has a save() method.

JavaScript
import mongoose from 'mongoose';

const userSchema = new mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  age: Number,
});

const User = mongoose.model('User', userSchema);

const ada = new User({ name: 'Ada', email: '[email protected]', age: 36 });
await ada.save();

That is the whole pattern. User maps to a collection named users (Mongoose lowercases and pluralizes the model name), and ada becomes one document in it.

Why not just use the driver?

The official mongodb driver is excellent and fast. It gives you collections, insertOne, find and so on, and it never gets in your way. Mongoose gives up a little of that rawness in exchange for features most applications end up writing by hand anyway:

Need Plain driver Mongoose
Check types and required fields Write your own checks Built into the schema
Default values Set them in your code default in the schema
Run code before or after save Write wrappers Middleware hooks
Relations between collections Manual lookups populate()
Reusable query logic Helper functions Statics, methods, query helpers
Casting "42" to 42 Manual Automatic casting

Neither choice is wrong. For a tiny script the driver may be enough; for a large application with many collections and a team of developers, Mongoose's shared structure pays for itself.

Mongoose does not replace the database

An important mental model: validation in Mongoose happens in your Node.js process, before the data is sent. MongoDB never sees the schema. If another program writes to the same collection with the plain driver, or you edit a document in a GUI, none of Mongoose's rules apply. For rules that must hold no matter who writes (for example uniqueness), you also need database-level features such as indexes, which later chapters cover.

flowchart LR
  A["Your Node.js code"] --> B["Mongoose<br/>schema, validation, hooks"]
  B --> C["MongoDB driver"]
  C --> D[("MongoDB server")]

Mongoose sits between your code and the official driver; the server never knows about the schema.

Try the idea: a schema as a checklist

Here is a stand-in, not Mongoose. It shows the core promise in about twenty lines: a schema is data that describes rules, and a function applies it.

Node.js A tiny schema checker (stand-in, not Mongoose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The first call returns an empty array: the data is fine. The second reports both a missing name and a wrong type for age. Real Mongoose does much more (it would first try to cast the string "36" to the number 36), but the idea is the same.

A second look: collections are pluralized names

Mongoose derives the collection name from the model name. This is a classic surprise for beginners, so here is a stand-in for the naming rule (the real library uses a pluralization library with more irregular cases).

Node.js Model name to collection name (simplified stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

You can always choose the name yourself with mongoose.model('User', schema, 'people') or the collection schema option, which you will meet in Schema options and defaults.

What this course covers

  1. Getting started: install, connect, your first model.
  2. Schemas and models: types, options, virtuals, methods, subdocuments.
  3. CRUD and queries: create, find, update, delete, pagination.
  4. Validation and middleware: rules, custom errors, hooks.
  5. Later chapters: population, aggregation, indexes, testing and projects continue in the track overview.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is Mongoose?

    Mongoose is an ODM: a library in your Node.js process that adds schemas, validation and helpers on top of the MongoDB driver.
  2. 2. Which Mongoose object is used to read and write a collection?

    A model is compiled from a schema and exposes methods such as create, find and updateOne for its collection.
  3. 3. Where does Mongoose schema validation run?

    The server does not know about Mongoose schemas. Other programs writing to the same collection are not checked by them.
  4. 4. What collection does mongoose.model('Category', schema) use by default?

    Mongoose lowercases the model name and pluralizes it, so Category becomes categories.

Summary

  • Mongoose is an ODM that adds structure to MongoDB from inside Node.js.
  • A schema describes a document, a model is built from it, and a document is one record.
  • Mongoose adds validation, defaults, middleware, population and a chainable query builder.
  • Schema rules live in your application, not in the database.
  • Model names map to lowercase plural collection names unless you override them.

Next: Installing Mongoose and connecting