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
npm
npm install mongoosepnpm
pnpm add mongooseyarn
yarn add mongooseMethods
No methods match this filter.
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. Theerrorsproperty 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 后按锚点提取参数块.
connect→ signature from https://mongoosejs.com/docs/api/mongoose.html; official text: “###Mongoose.prototype.connect()##### Parameters: -uri«string» mongodb URI to connect to -[options]«object» passed down to the Mong”Schema→ signature from https://mongoosejs.com/docs/api/schema.html; official text: “###Schema()##### Parameters: -[definition]«object|Schema|Array» Can be one of: object describing schema paths, or schema to copy, or array of objects and sc”model→ signature from https://mongoosejs.com/docs/api/mongoose.html; official text: “###Mongoose.prototype.model()##### Parameters: -name«string|Function» model name or class extending Model -[schema]«Schema» the sche”find→ signature from https://mongoosejs.com/docs/api/model.html; official text: “###Model.find()##### Parameters: -filter«object|ObjectId» -[projection]«object|string|Array[string]» optional fields to return, see Query.prototype.s”pre / post hooks→ signature from https://mongoosejs.com/docs/api/schema.html; official text: “###Schema.prototype.pre()##### Parameters: -methodName«string|RegExp|Array[string]» The method name or regular expression to match method name”
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.