( MONGOOSE INSTRUMENTATION )ORM Performance Safeguards

Find slow Mongoose queries before they hit production

Mongoose abstraction shields you from raw query patterns, making it easy to accidentally introduce unindexed filters. TraceMole hooks into the Mongoose driver lifecycle to audit explain plan stats when queries exceed latency limits.

Key Features

Plugin-free Monitoring

No need to attach plugins to every schema. We wrap driver-level database execution hooks, capturing queries across all collections automatically.

Threshold-based Auditing

Configure slow-query latency limits (e.g. 100ms) to trigger explain runs only for query structures that are slow, avoiding telemetry bloat.

Why MongoDB queries become slow

Mongoose acts as an object modeling layer that hides raw MongoDB syntax, which can lead to developers writing query selectors without knowing how the database processes them. If a selector doesn't align with an existing index, MongoDB does a collection scan to inspect each document. Linear scans degrade response times and increase CPU utilization as database records grow.

How TraceMole detects COLLSCANs

TraceMole instruments Mongoose query executions at the driver database level. When query latency exceeds acceptable thresholds, TraceMole runs an asynchronous explain plan check. We analyze the resulting execution stages in the winning plan. If a COLLSCAN is detected, we immediately flag the offending schema code line and notify you.

Mongoose Setup Example

Wrap your Mongoose connection instance at the database initialization entry point:

import mongoose from "mongoose";
import { withTraceMoleMongo } from "@tracemole/mongodb-wrapper";

// Connect Mongoose to MongoDB
await mongoose.connect(process.env.MONGODB_URI);

// Instrument connection driver for explain analytics
const connection = withTraceMoleMongo(mongoose.connection.db);
export default connection;