mongoose
Mongoose is the most popular Object Data Modeling (ODM) library for MongoDB and Node.js, providing a schema-based solution for modeling application data. Mongoo…
Installation
npm install mongoose
yarn add mongoose
pnpm add mongoose
Import
import mongoose from 'mongoose';
Quick Example
import mongoose from 'mongoose';
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, unique: true },
});
const User = mongoose.model('User', userSchema);
await mongoose.connect('mongodb://localhost/mydb');
const user = await User.create({ name: 'Alice', email: '[email protected]' });About mongoose
Mongoose is the most popular Object Data Modeling (ODM) library for MongoDB and Node.js, providing a schema-based solution for modeling application data. Mongoose translates between JavaScript objects and the documents stored in MongoDB, enforcing structure through schemas that define the shape of documents, their data types, validation rules, default values, and virtuals (computed properties). Schemas support built-in validators (required, min, max, enum, match), custom validators, middleware hooks (pre/post for save, validate, remove, find), and plugins for reusable functionality. Mongoose provides a powerful query builder with chainable methods for find, sort, limit, skip, populate (JOIN-like reference resolution), lean (returning plain objects for performance), and aggregation pipeline support. The library handles connection pooling, automatic reconnection, and supports both callback and promise-based APIs. Mongoose's populate feature resolves references between collections, providing JOIN-like functionality that MongoDB natively lacks. The discriminator feature enables single-collection inheritance patterns. Mongoose is the standard way to interact with MongoDB from Node.js applications and is deeply integrated into frameworks like NestJS and Express.
Quick Facts
| Package | mongoose |
| Category | Database |
| Weekly Downloads | 3M+ |
| License | MIT |
| Install | npm install mongoose |
Related Packages
mongodb is the official MongoDB Node.js driver that provides a direct interface to MongoDB databases…
Prisma is a next-generation Node.js and TypeScript ORM that provides a declarative data modeling lan…
TypeORM is a full-featured ORM for TypeScript and JavaScript that supports both Active Record and Da…
Sequelize is a promise-based Node.js ORM that supports PostgreSQL, MySQL, MariaDB, SQLite, and Micro…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.