mongoose

Elegant MongoDB object modeling for Node.js — schemas, validation, middleware, and population for working with MongoDB.

mongoose

Node.js

Elegant MongoDB object modeling for Node.js — schemas, validation, middleware, and population for working with MongoDB.

Fit

Bundle (gzip)

346.1 kB

1.9 MB raw

Install size includes transitive dependencies. Bundle size is gzipped browser payload.

Freshness

The Problem

MongoDB's schemaless nature is flexible but leads to inconsistent documents and no validation layer. Using the raw mongodb driver means writing all query building, casting, and validation yourself.

What It Does

Mongoose defines schemas that describe document shape and enforce types, defaults, and validation rules. Models provide a rich query API with chainable methods, and middleware hooks let you run code before/after save, delete, and find operations. Population replaces ObjectId references with full documents automatically.

Installation

bash
npm install mongoose

Usage Example

typescript
import mongoose, { Schema, Document } from 'mongoose';

interface IUser extends Document {
  email: string;
  name: string;
  createdAt: Date;
}

const UserSchema = new Schema<IUser>({
  email: { type: String, required: true, unique: true, lowercase: true },
  name: { type: String, required: true },
  createdAt: { type: Date, default: Date.now },
});

const User = mongoose.model<IUser>('User', UserSchema);

await mongoose.connect(process.env.MONGODB_URI!);
const user = await User.create({ email: 'alice@example.com', name: 'Alice' });
const found = await User.findOne({ email: 'alice@example.com' });

Related packages