$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[September 26, 2026] MongoDB

MongoDB Performance Optimization

MongoDB Performance: Tips & Tricks

────────────────────────────────────────────────────────

MongoDB Performance: Tips & Tricks

Tip: Explain Plans

Post::raw(function ($collection) {
    $cursor = $collection->find(['title' => 'Test']);
    return $cursor->explain('executionStats');
});

Check if your query uses an index or does a collection scan (COLLSCAN).

Gotcha: Projection for Smaller Results

$collection->find(
    ['published' => true],
    ['projection' => ['title' => 1, 'slug' => 1]]
);

Only fetch the fields you need.

Tip: Avoid $where

// Bad — JavaScript evaluation, no index
$collection->find(['$where' => 'this.views > 100']);

// Good — uses index
$collection->find(['views' => ['$gt' => 100]]);

Gotcha: Bulk Operations for Batch Inserts

$bulk = new MongoDB\Driver\BulkWrite();
for ($i = 0; $i < 1000; $i++) {
    $bulk->insert(['title' => "Post {$i}"]);
}
$collection->executeBulkWrite($bulk);

Much faster than 1000 individual inserts.

Tip: Covered Queries

If all query fields are in the index, MongoDB never reads the documents:

$collection->createIndex(['slug' => 1, 'title' => 1]);
$collection->find(['slug' => 'test'], ['projection' => ['title' => 1, '_id' => 0]]);

Gotcha: Sort Without Index

Sorting without an index uses memory. Add an index for the sort field.

Tip: Monitor with serverStatus()

DB::connection('mongodb')->command(['serverStatus' => 1]);

Check connections, operations, memory usage.

Gotcha: WiredTiger Cache

MongoDB uses 50% of RAM minus 1GB for its cache. Monitor cache utilization.

Tip: Embed or Reference? The 80/20 Rule

If you always access data together, embed it. If you access it independently, reference it. The 16MB document size limit is the hard boundary — stay under 1MB for most documents.

Tip: Index Your Query Patterns, Not All Fields

Creating indexes on every field wastes RAM. Use explain() to find in-memory sorts and collection scans. Index only what your actual queries filter on.

Gotcha: No Transaction Rollback for Index Builds

Building an index on a large collection can take hours. If it fails midway, the partial index is silently discarded. Plan index builds during maintenance windows.

Senior Insight

MongoDB performance optimization starts with the working set — the data and indexes that must be in memory. If your working set exceeds available RAM, performance degrades dramatically as MongoDB pages data in and out. I monitor workingSet metrics and plan for the working set to fit in RAM. The most impactful optimization I've made: ensuring indexes are fully in memory by monitoring indexMissRatio. A 1% index miss ratio on a busy system causes visible latency.

Source: MongoDB Developer Center (https://www.mongodb.com/developer/), MongoDB Engineering Blog (https://www.mongodb.com/blog/channel/engineering-blog), Studio 3T Blog (https://studio3t.com/blog/)

────────────────────────────────────────────────────────
<-- back to posts