Stop 08 · ORMs & Query BuildersTopics · 5/10

Mongoose

Part of the Backend Developer Roadmap

In short

MongoDB lets any document in. Mongoose adds a schema on top, so every document your app saves has the right fields, types and defaults.

The dominant ODM (Object-Document Mapper) for MongoDB in Node.js — adds schemas, validation and a familiar model-based API on top of MongoDB's flexible documents.

The blob as a hostel warden checks bags at the gate with a clipboard and whistle.

Think of it like this

The hostel warden at the gate

The hostel building doesn't care who walks in. The warden does: ID card, back before 10, no pressure cookers. Mongoose is that warden for MongoDB. The database would accept anything, but Mongoose checks each document against your rules before letting it in.

Watch it happen

Validation before saving

API
Mongoose
MongoDB

Student.create({ name })

1/6The API tries to save a student without an email.

The big ideas

What you need to know

Schemas and models

A schema lists fields, types and rules. A model is the class you use to query that collection.

Validation

required, min, max, enum and match run before saving. Custom validators handle anything else.

populate()

Store an ObjectId reference, then populate() swaps it for the referenced document.

Middleware and virtuals

Run code before save, like hashing a password, and add computed fields like fullName.

See it in code

Walk through the code

ts
1import mongoose, { Schema, model } from "mongoose";2 3const studentSchema = new Schema(4  {5    name: { type: String, required: true, trim: true },6    email: { type: String, required: true, unique: true, lowercase: true },7    year: { type: Number, min: 1, max: 5, default: 1 },8    hostel: { type: Schema.Types.ObjectId, ref: "Hostel" },9  },10  { timestamps: true },11);12 13export const Student = model("Student", studentSchema);14 15await mongoose.connect("mongodb://localhost:27017/college");16const riya = await Student.findOne({ email: "riya@college.edu" }).populate("hostel");

1/4Each field has a type and rules. Saving without a name or email fails validation.

unique: true builds a unique index, not a validator. Duplicates show up as a duplicate key error with code 11000.

Save yourself the pain

Common mistakes

Oops

Treating unique: true as validation.

Do this instead

It's an index. Make sure it's built, and handle error code 11000 for duplicates.

Oops

Using populate() for everything, like SQL joins.

Do this instead

Each populate is another query. Embed data you always need; populate only the occasional extra.

Your mission

A warden for your data

~25 min

Quick check

Why use Mongoose if MongoDB has no fixed schema?

Go deeper

  1. 1Define a Mongoose schema with validation rules for a collection.
  2. 2Practice Mongoose's query methods and compare them to raw MongoDB driver calls.
  3. 3Learn Mongoose middleware (`pre`/`post` hooks) for logic that runs around saves/queries.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge