Ch. 12 · MongoDB

MongoDB Capped Collections

Use capped collections for fixed-size, insertion-ordered logs, and know why documents cannot grow.

~2 min readintermediateupdated Oct 5, 2026

A capped collection has a fixed size and preserves insertion order, overwriting the oldest documents when it fills. It is designed for logs and streams where recent data matters and old data can be discarded automatically.

Before you start

You should be comfortable with collections and inserts. This article uses the MongoDB shell.

Step-by-step walkthrough

Step 1: Create it with a size bound

db.createCollection('logs', { capped: true, size: 10485760 }) fixes the collection at about ten megabytes, and an optional max caps the document count too. Once full, new inserts overwrite the oldest documents, so storage is bounded without a cleanup job.

Step 2: Rely on insertion order

Documents are returned in insertion order by default, which makes a capped collection a natural ordered log. This ordering is guaranteed, unlike a normal collection where you must sort explicitly.

Step 3: Respect the no-growth rule

Because space is preallocated per document slot, a document cannot grow, so updates that increase size fail. Capped collections suit append-only data; for mutable documents use a regular collection or a time-series collection instead.

Worked scenario

The capped collection keeps only the most recent log entries.

db.createCollection('logs', { capped: true, size: 10485760, max: 100000 });
db.logs.insertOne({ at: new Date(), message: 'service ready' });
db.logs.find().sort({ $natural: 1 });
JavaScript

Walk through the example

The collection holds up to ten megabytes or one hundred thousand documents, whichever comes first, and then overwrites the oldest. sort({ $natural: 1 }) reads in insertion order, which is the natural guarantee. An update that grows a document beyond its slot is rejected.

Common mistake

Using a capped collection as general storage and then discovering updates fail when a field grows. Another is expecting it to replace retention policy: it bounds size, but the oldest data is lost without an archive, which may not be acceptable for audit logs.

Verify the behavior

Insert more documents than the cap allows and confirm the oldest are gone and the count stays bounded. Attempt to grow a document with an $set that lengthens a string and confirm the failure. Confirm reads return insertion order without an explicit sort.

Interview exercise

When is a capped collection a better fit than a TTL index?

Answer and reasoning

When you want a strict bound on size or count and a guaranteed insertion order, such as a rolling log or a change feed. A TTL index removes documents after a time window but has no size bound and no ordering guarantee. Use capped for “keep the last N” and TTL for “keep for N days”.

Continue learning

Compare retention in TTL expiration and streaming in Change streams. Read the MongoDB capped collections documentation and try the MongoDB interview questions.

More in MongoDB

read ✓MongoDB · mid

MongoDB Bulk Writes

Batch inserts and updates with bulkWrite, choose ordered or unordered, and handle partial failures correctly.

~2 min readread →
read ✓MongoDB · hard

MongoDB Change Streams and Resume Tokens

React to inserts and updates with change streams, persist resume tokens, and handle invalidate and failover correctly.

~2 min readread →
esc