API Reference

Mongoose

Elegant MongoDB object modeling for Node.js with schemas, validation, middleware, and rich query building.

Official Documentation ↗
CompatibilityThis documentation covers version v8.6. Check the official docs for the latest API surface.

Installation

npmnpm install mongoose
pnpmpnpm add mongoose
yarnyarn add mongoose

Methods

Mongoose provides a straight-forward, schema-based solution to model application data in MongoDB. It includes built-in type casting, validation, query building, business logic hooks, and virtuals.

Key Features

  • Schemas: Typed field definitions with defaults, validators, and indexes
  • Models: Compiled query constructors with create, find, update, aggregate
  • Middleware: pre/post hooks for save, validate, find, and remove
  • Population: Reference resolution across collections
  • Subdocuments: Nested schemas and arrays with their own validation

Errors

The official API reference declares the following error types:

  • MongooseError (class) — official text: “MongooseError constructor. MongooseError is the base class for all Mongoose-specific errors.” (source: https://mongoosejs.com/docs/api/error.html, fetched 2026-11-08T16:01:12.9137723+08:00)
  • Error.CastError (property(constructor)) — official text: “An instance of this error class will be thrown when mongoose failed to cast a value.” (source: https://mongoosejs.com/docs/api/error.html, fetched 2026-11-08T16:01:12.9137723+08:00)
  • Error.ValidationError (property(constructor)) — official text: “An instance of this error class will be thrown when validation failed. The errors property contains an object whose keys are the paths that failed and whose values are instances of CastError or ValidationError.” (source: https://mongoosejs.com/docs/api/error.html, fetched 2026-11-08T16:01:12.9137723+08:00)
  • Error.MongooseServerSelectionError (property) — official text: “Thrown when the MongoDB Node driver can’t connect to a valid server to send an operation to.” (source: https://mongoosejs.com/docs/api/error.html, fetched 2026-11-08T16:01:12.9137723+08:00)

API reference evidence

Fetched from official sources on 2026-11-08T16:01:12.9137723+08:00 via WebFetch 官方 API 页 + PowerShell 下载 mongoose/model/schema/error HTML 后按锚点提取参数块.

FAQ

What is the difference between a Schema and a Model?
A Schema defines the shape, types, and validation of documents. A Model is a compiled class from a Schema that provides query methods like find, create, and update to interact with a MongoDB collection.
How does populate differ from a SQL join?
populate replaces referenced ObjectIds with the referenced document via additional queries, mimicking joins. Mongoose 7+ also supports populate with lean and nested paths, but it is not a single server-side join.
Should I use lean() for read-heavy queries?
Yes. lean() returns plain JavaScript objects instead of Mongoose documents, skipping hydration and making reads 2-3x faster and lighter on memory for read-only results.