$ lexprog.com

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

[September 04, 2025] MongoDB

MongoDB Capped Collections: Logging

MongoDB Capped Collections: Logging

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

MongoDB Capped Collections: Logging

Tip: Create Capped Collection

DB::command([
    'create' => 'logs',
    'capped' => true,
    'size' => 10485760, // 10MB
    'max' => 10000,     // max documents
]);

Gotcha: Capped Collections Are Fixed Size

When the size limit is reached, oldest documents are automatically removed.

Tip: Natural Order

$collection->find()->sort(['$natural' => -1])->limit(10);

Returns the most recent documents in insertion order.

Gotcha: No Delete Operations

You can't delete individual documents from a capped collection. Only drop() removes everything.

Tip: Tailable Cursors

$cursor = $collection->find([], ['cursorType' => MongoDB\Operation\Find::TAILABLE]);

Waits for new documents to be inserted. Like tail -f for MongoDB.

Gotcha: No Index on _id (Optional)

Capped collections don't require an _id index. Skip it to save space.

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

Capped collections are fixed-size collections that automatically evict old documents. I've used them for logging and metrics where only the most recent data matters. The key detail: capped collections preserve insertion order — documents are always returned in insertion order even without a sort. But you cannot remove documents or update them to increase size. For logging in high-throughput applications, capped collections are a perfect fit.

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