Facebook Pixel

Common MongoDB Performance Mistakes (and How to Fix Them)

MongoDB performance has predictable failure modes. Here are common mistakes and fixes.

Common MongoDB Performance Mistakes

MongoDB performance has predictable failure modes. Here are common mistakes and how to fix them.

Mistake 1: No Indexes on Hot Queries

You query by email but did not index it. MongoDB scans every document. Fix: index the fields you query. Use explain() to find slow queries.

Mistake 2: Using skip for Large Offsets

User.find().skip(100000).limit(20) is slow. MongoDB scans the first 100000 docs. Fix: use cursor-based pagination. Find documents where createdAt is less than the last cursor.

Mistake 3: Returning All Fields

You do User.find() and return everything, including large fields you do not need. Fix: use .select() to return only the fields you need. Use .lean() for read-only.

Mistake 4: N+1 Queries

You loop over documents and call findById for each reference. Fix: use populate to do it in 2 queries.

Mistake 5: Unbounded Arrays in Documents

You embed all messages in a connection document. The array grows until the document hits 16MB. Fix: use refs and a separate collection for large N.

Mistake 6: $where and Unindexed $regex

$where runs JavaScript (slow). $regex on an unindexed field scans the collection. Fix: avoid $where. Use a text index for search, or index the field if you need regex.

Mistake 7: Not Using Compound Indexes

You have separate indexes on A and B, but you query on A+B. MongoDB uses one index per query. Fix: add a compound index on (A, B).

Mistake 8: Indexing Every Field

Every index slows writes. Fix: index only the queries you actually run. Use explain() to decide.

Mistake 9: countDocuments on Large Collections

countDocuments can be slow without an index. Fix: if you only need to know if documents exist, use findOne and check for null. For totals, cache the count in Redis.

Mistake 10: Hydrating Full Documents for JSON

You do User.find() and send the result as JSON. Mongoose hydrates full documents with methods. Fix: use .lean() to get plain objects. Faster and uses less memory.

Mistake 11: No Connection Pooling

You open a new connection per request. Fix: use one connection (Mongoose does this by default). Set poolSize to match expected concurrency.

Mistake 12: Sync Calls in Request Handlers

You use callbacks or sync APIs. Fix: use async/await. Let Mongoose's promises flow through asyncHandler.

The Takeaway

Common MongoDB performance mistakes: no indexes on hot queries, skip for large offsets, returning all fields, N+1 queries, unbounded arrays, $where and unindexed $regex, no compound indexes, indexing every field, countDocuments on large collections, hydrating full documents, no connection pooling, and sync calls. Use explain() to find and fix them.

No indexes on hot queries, skip for large offsets, returning all fields, N+1 queries, unbounded arrays, $where and unindexed $regex, no compound indexes, indexing every field, countDocuments on large collections, hydrating full documents, no connection pooling, and sync calls.

MongoDB still scans the skipped documents before returning the ones you want. For offset 100000, it scans 100000 docs. Use cursor-based pagination instead: find documents where createdAt is less than the last cursor.

Use populate. Instead of looping over documents and calling findById for each reference, use Model.find().populate('refField'). Mongoose does it in 2 queries (one for the parent, one for all referenced docs).

It skips hydrating full Mongoose documents and returns plain JS objects. Faster and uses less memory. Use for read-only queries where you only need to send JSON. Do not use it if you need to call instance methods or save the document.

Every index adds write overhead (each insert/update/delete updates the index), storage, and memory. Index only the queries you actually run. Use explain() to decide which indexes are worth it.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.