Debug slow MongoDB queries in Next.js APIs
API latency in Next.js is frequently caused by unindexed database filters. TraceMole connects Next.js serverless API routes and Server Actions directly with database explain outputs, highlighting slow index stages instantly.
Next.js Advantages
Lightweight SDK footprint designed specifically for Vercel, AWS Lambda, or serverless next-generation runtimes with fast cold start times.
View serverless API route transactions side-by-side with underlying database query execution timings in our nested waterfall graphs.
Connect MongoDB explain stats to API traces
When working with serverless environments like Next.js API routes or Server Actions, standard performance tracking only gives you total request duration. TraceMole hooks directly into the database client event emitter, letting you link actual explain plans and query execution details to specific Next.js request lifecycle transactions and OpenTelemetry tracing logs.
Track docsExamined vs nReturned
Monitoring Next.js API performance means checking database query efficiency metrics closely. By comparing totalDocsExamined against nReturned inside MongoDB execution plans, TraceMole highlights query routes that perform collection scans, scanning thousands of items just to yield a couple of results.
Next.js Configuration
If you are using Next.js 15 or newer, the instrumentation hook is active by default, and you can skip this config. For Next.js 13 & 14, enable the experimental instrumentation hook inside your `next.config.ts` or `next.config.js`:
// next.config.ts setup
const nextConfig = {
experimental: {
instrumentationHook: true,
},
};
export default nextConfig;