Back to Blog
MongoDBPerformanceNode.jsDatabaseOptimizationBackend

How to Fix Slow MongoDB Queries: A Practical Performance Guide

How to Fix Slow MongoDB Queries: A Practical Performance Guide

What Causes Slow MongoDB Queries?

If you notice your application slowing down as your data grows, the culprit is often your MongoDB queries. Here are the most common causes:

  • Missing Indexes: Every query without an index causes a full Collection Scan (COLLSCAN).
  • Using unescaped $regex: It prevents index usage unless it starts with a caret (^) or doesn't use leading wildcards.
  • Fetching too much data: Using find() without specifying the required fields (projections).
  • N+1 Queries: Making repeated database queries inside loops.

How to Diagnose Slow Queries?

MongoDB provides built-in tools to diagnose and profile performance:

// Enable Database Profiler to track slow queries
db.setProfilingLevel(1, { slowms: 100 })

// View the latest slow queries
db.system.profile.find().sort({ ts: -1 }).limit(5).pretty()

// Explain the query execution plan
db.collection.find({ name: "test" }).explain("executionStats")

Practical Solutions to Speed Up MongoDB

1. Adding the Right Indexes

The first and most important step to accelerate your queries:

// Single field index
db.users.createIndex({ email: 1 })

// Compound Index for multi-field queries
db.orders.createIndex({ userId: 1, createdAt: -1 })

// Text Index for full-text search
db.posts.createIndex({ title: "text", content: "text" })

2. Optimizing Search Queries ($regex)

Make sure to escape special characters to prevent NoSQL Injection and improve performance:

function escapeRegex(s) {
  return s.replace(/[.*+?^{}()|[]\\]/g, '\\$&');
}

// Use Prefix Regex to leverage indexes
const safe = escapeRegex(search);
filter.name = { $regex: '^' + safe, $options: 'i' }

3. Using Cursor-based Pagination Instead of Skip

Using skip with large datasets causes extreme slowness. Use Range-based (Cursor) Pagination instead:

// Instead of:
db.users.find().skip(10000).limit(20)

// Use:
db.users.find({ _id: { $gt: lastId } }).limit(20).sort({ _id: 1 })

4. Specifying Required Fields (Projection)

Don't fetch data you don't need:

// Instead of:
db.users.find({ role: "admin" })

// Use:
db.users.find({ role: "admin" }, { name: 1, email: 1, _id: 1 })

Performance Comparison: Before vs. After Optimization

QueryBefore OptimizationAfter Optimization
Search across 100,000 documents850ms2ms
Aggregation3200ms45ms
Pagination (Page 500)1200ms3ms

Conclusion

Optimizing MongoDB performance isn't difficult, but it requires understanding how indexes and query patterns work. By following these best practices, you will notice a significant improvement in your application's speed.

Are you facing performance issues in your current application? Contact me to analyze and resolve your performance bottlenecks.

About the Author

Abdallah Dev

Full Stack Web Developer passionate about building modern web applications and sharing knowledge.

Ready to Build Something Great?

Let's discuss your project and find the perfect solution for your needs. Get a free consultation today.

Get Free Consultation