mongoose
| Property | Value |
|---|---|
| Package | mongoose |
| Versions Covered | >=5.0.0 |
| Contract Version | 1.0.0 |
| Status | production |
| Last Verified | 2026-02-25 |
| Maintainer | corpus-team |
Installation
npm install mongoose
Covered Functions
This contract covers 16 function(s):
find()
Queries the database and returns an array of documents matching the filter
Import:
import { find } from 'mongoose';
Postconditions
What happens after calling this function:
🔴 ERROR - connection-error-no-try-catch
Condition: Database connection failed or was interrupted
Throws: MongooseError or MongoError
Required Handling:
Caller MUST wrap in try-catch or use .catch(). Handle connection errors separately from query results. Implement retry logic for transient connection issues.
📖 Source
🔴 ERROR - cast-error-no-try-catch
Condition: Query parameter cannot be cast to expected type
Throws: CastError
Required Handling:
Caller MUST wrap in try-catch or use .catch(). Validate query parameters before passing to mongoose. Handle CastError separately from other errors.
📖 Source
findOne()
Queries the database and returns a single document matching the filter
Import:
import { findOne } from 'mongoose';
Postconditions
What happens after calling this function:
🔴 ERROR - connection-error-no-try-catch
Condition: Database connection failed
Throws: MongooseError or MongoError
Required Handling:
Caller MUST wrap in try-catch or use .catch() to handle connection errors
📖 Source
🔴 ERROR - cast-error-no-try-catch
Condition: Query parameter casting failed
Throws: CastError
Required Handling:
Caller MUST wrap in try-catch to handle type casting errors
📖 Source
findById()
Finds a single document by its _id field
Import:
import { findById } from 'mongoose';
Postconditions
What happens after calling this function:
🔴 ERROR - cast-error-no-try-catch-invalid-id
Condition: Provided ID is not a valid ObjectId format
Throws: CastError with kind: 'ObjectId'
Required Handling:
Caller MUST wrap in try-catch or use .catch(). Common when user input is passed directly as ID. Validate ID format before querying.
📖 Source
🔴 ERROR - connection-error-no-try-catch
Condition: Database connection failed
Throws: MongooseError
Required Handling:
Caller MUST handle connection errors
📖 Source
create()
Creates one or more documents in the database
Import:
import { create } from 'mongoose';
Postconditions
What happens after calling this function:
🔴 ERROR - validation-error-no-try-catch
Condition: Document validation failed (missing required fields, wrong types, custom validators)
Throws: ValidationError with errors object containing field-specific ValidatorErrors
Required Handling:
Caller MUST wrap in try-catch or use .catch(). Check error.name === 'ValidationError' and inspect error.errors object. Each field error is accessible via error.errors[fieldName].
📖 Source
🔴 ERROR - duplicate-key-error-no-try-catch
Condition: Unique constraint violation (E11000 error)
Throws: MongoError with code: 11000
Required Handling:
Caller MUST wrap in try-catch and check error.code === 11000. This is NOT a ValidationError - it comes from MongoDB driver. Extract duplicate field from error message or error.keyPattern.
📖 Source
🔴 ERROR - connection-error-no-try-catch
Condition: Database connection failed
Throws: MongooseError
Required Handling:
Caller MUST handle connection errors with retry logic
📖 Source
insertMany()
Inserts multiple documents into the database in a single operation
Import:
import { insertMany } from 'mongoose';
Postconditions
What happens after calling this function:
🔴 ERROR - validation-error-no-try-catch
Condition: One or more documents fail validation
Throws: ValidationError
Required Handling:
Caller MUST wrap in try-catch. Default behavior stops on first error; use ordered:false to continue on errors.