How I Fixed a Node.js API That Was Taking 15 Minutes to Return 8,000 Records
The ticket came in with one line: "The API is too slow." No stack trace. No logs. Just a frustrated client and an endpoint that took around 15 minutes to return 8,000 records. Fun. The worst part? ...

Source: DEV Community
The ticket came in with one line: "The API is too slow." No stack trace. No logs. Just a frustrated client and an endpoint that took around 15 minutes to return 8,000 records. Fun. The worst part? The code wasn't even mine. Someone else had written it, shipped it, and moved on. Now I was the one staring at a Mongoose query wondering where 15 minutes were disappearing to. Here's exactly what I investigated, what I found, and how I brought it down to around 15 seconds β without rewriting everything from scratch. The Setup The API was a Node.js service using Express and Mongoose, backed by MongoDB Atlas. It had a /reports endpoint that fetched records and ran some calculations for a dashboard. Simple enough on paper. Here's what the original route looked like: // Original route β the culprit app.get('/reports', async (req, res) => { try { const data = await Report.find({ status: req.query.status, createdAt: { $gte: new Date(req.query.from), $lte: new Date(req.query.to) } }); res.json(d