$ lexprog.com

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

[July 29, 2025] MongoDB

MongoDB Indexing Strategies

MongoDB Indexing: Tips & Tricks

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

MongoDB Indexing: Tips & Tricks

Tip: Single Field Index

Post::raw(function ($collection) {
    $collection->createIndex(['slug' => 1]);
});

1 = ascending, -1 = descending. Doesn't matter for single fields.

Gotcha: Compound Index Order Matters

$collection->createIndex(['category_id' => 1, 'created_at' => -1]);

This index supports queries on category_id alone, but NOT created_at alone.

Tip: Text Index for Search

$collection->createIndex(['title' => 'text', 'content' => 'text']);

Enables $text queries. Only one text index per collection.

Gotcha: Unique Index on Nullable Fields

MongoDB allows only one null value in a unique index. Use a sparse index instead:

$collection->createIndex(['slug' => 1], ['sparse' => true]);

Tip: TTL Index for Auto-Expiration

$collection->createIndex(['created_at' => 1], ['expireAfterSeconds' => 86400]);

Documents are automatically deleted after 24 hours.

Gotcha: Indexes Slow Down Writes

Every index adds overhead to inserts and updates. Only index what you query.

Tip: Partial Index

$collection->createIndex(
    ['slug' => 1],
    ['partialFilterExpression' => ['published' => true]]
);

Only indexes published posts. Smaller index, faster.

Gotcha: Background Index Creation

$collection->createIndex(['slug' => 1], ['background' => true]);

In older MongoDB versions, this prevented blocking. In 4.2+, all index builds are non-blocking.

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 indexing requires more discipline than PostgreSQL because the query planner has fewer index options. I always use explain('executionStats') to verify index usage — a collection scan on a 10M document collection is devastating. The common mistake: creating indexes on every field 'just in case', wasting RAM. MongoDB stores indexes in memory (hot data), so every unnecessary index consumes RAM that could cache working data. Index your query patterns, not your schema.

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