mongoose
Elegant MongoDB object modeling for Node.js — schemas, validation, middleware, and population for working with MongoDB.
mongoose
Node.jsElegant 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
npm install mongooseUsage Example
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
Role and attribute based access control (RBAC + ABAC) for Node.js with a fluent, readable API.
bcryptjsNode.jsPure JavaScript bcrypt password hashing — no native dependencies, same API as bcrypt, works anywhere Node.js runs.
bullmqNode.jsPremium message queue and job scheduler for Node.js backed by Redis — reliable background job processing with retries, priorities, and rate limiting.
casbinNode.jsA powerful authorization library that supports RBAC, ABAC, ACL and more via configurable policy models.
dotenvNode.jsLoad environment variables from a `.env` file into `process.env` — the standard way to manage configuration in Node.js apps.