🗄️
Database3M+/wkMIT

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
npm install mongoose
yarn
yarn add mongoose
pnpm
pnpm add mongoose

Import

ESM
import mongoose from 'mongoose';

Quick Example

usage
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

Packagemongoose
CategoryDatabase
Weekly Downloads3M+
LicenseMIT
Installnpm install mongoose

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.