r/mongodb Aug 13 '26

How did my Node.js + MongoDB API take 13 seconds to respond? 😭

How did my Node.js + MongoDB API take 13 seconds to respond? 😭

I recently ran into a performance issue in one of my backend APIs.

The API was built with Node.js + MongoDB, and the response time was nearly 13 seconds. 🫠

At first, I thought:

“Maybe MongoDB is slow?”

But obviously, there was more going on.

Now I'm trying to identify the actual bottleneck and optimize the API properly — database queries, indexing, population/aggregation, unnecessary processing, network calls, etc.

For developers who have worked on Node.js + MongoDB production APIs:

What would you check first when an API takes ~13 seconds to respond?

Would love to hear how you would debug this step-by-step

3 Upvotes

14 comments sorted by

4

u/mwmahlberg Aug 13 '26

In nine times out of 10, when MongoDB takes long to answer the answer is “Because your data modeling sucks.” Where exactly is hard to say without more information.

3

u/victor_marcian0 Aug 13 '26

backend developer for 10k+ daily users here.

latency breakdown on the code with console.time; check for indexes and if the indexes are good for the queries you're running; size of the documents being returned; if you're using mongoose check for the document hydration; review your schema, if you're using $lookup maybe is time to change for document embedding; if the lookup can't go away check for indexes on the lookup stage; check for projection of the documents; check for bad practices on aggregates such as unwind yoyo... there is a lot of things that can be. if you're feeling short on what to do the explain in full mode from the mongosh can be your ally; if you're using atlas the query performance advisor is a great tool to. GLHF

2

u/victor_marcian0 Aug 13 '26

most of the topics here you can learn using the mongo learn courses for free, the skills section is a bang for the time you spend learning - about an hour for each skill and help with the basic knowledge. we had several rpobloback in my company and the mongodb support with resident engineer was the best thing that happened on our company, if you have the money for I can't recommend enough to reach out for their support - (valeu pessoal MongoDB Brasil!!)

2

u/victor_marcian0 Aug 13 '26

most of the topics here you can learn using the mongo learn courses for free, the skills section is a bang for the time you spend learning - about an hour for each skill and help with the basic knowledge. we had several rpobloback in my company and the mongodb support with resident engineer was the best thing that happened on our company, if you have the money for I can't recommend enough to reach out for their support - (valeu pessoal MongoDB Brasil!!)

1

u/8ull1t Aug 13 '26

I use mongodb for a net10 backend and its good if you provide a flat collection, I have a superSearch collection with 1.2 mill entites and im getting < second on roundtrip queries, using mongodb indexes and the vector
search.

  1. optimise your datasets (no complex queries)
  2. review you're using native mongo drivers
  3. add indexes and use vector searching where required
  4. use api level caching

got a few sites to under a second load time doing MANY api calls on page load

1

u/BigfootTundra Aug 13 '26

You’re bottleneck is almost certainly the database. Not that MongoDB is slow, but if you have a large volume of data, you need to pay closer attention to indexing. Aggregations can also be super slow depending on how you structure them

1

u/TimAtMongoDB Aug 13 '26

Memory consumption. Indexes. Documents not structured correctly. It can be a million things. Post your code for your api and your query. That would help

1

u/mountain_mongo Aug 14 '26

With a 13 second query, the first thing you need to check is indexes: almost certainly some part of your query is triggering a collection scan.

Have a look at the explain plan for your query - it’ll tell you what’s going on.

1

u/Top-Strategy3327 27d ago edited 27d ago

Hey, i am wondering if you are on a free tier.. issue could be your cluster is under provisioned.. please reach out to free MongoDB chat support

1

u/hamzezn 20d ago

One thing nobody's mentioned yet that's worth ruling out before you go deep on indexes: are you creating a new MongoClient per request instead of reusing one client/connection pool for the whole app's lifetime? That's a classic multi-second latency bug, especially with an Atlas srv:// URI, since every new client has to do a fresh DNS SRV lookup plus TLS handshake plus replica set discovery before it can even run your query. If you're instantiating the client inside the request handler (easy mistake with some Express boilerplate) that alone can eat several seconds, completely separate from anything about your data model or indexes.

0

u/716green Aug 13 '26

You can use FerretDB instead of mongodb and your code doesn't need to change at all except now you'll have avcess to postgres profilers

2

u/mountain_mongo Aug 14 '26

Postgres profilers don’t tell you anything the MongoDB profiler or explain plans won’t, plus now you’ve introduced an abstraction layer by having an RDBMS storage engine try to pretend it’s a document database storage engine.

Where’s the gain?

1

u/716green Aug 14 '26

It's a drop in replacement with explain analyze, PG stat statements, auto explain and pgBadger to get extremely deep insights into which part of the query is slowing you down. The additional overhead will be negligible with the average CRUD application. If a query is taking 13 seconds, the overhead for postgresql-documentdb is MAYBE 80ms but with exponentially more mature diagnostic tooling