What Is a Capped Collection in MongoDB?
Learn what a capped collection is in MongoDB, how its fixed-size circular buffer works, why the oplog uses it, and when to use one in your app.
Expected Interview Answer
A capped collection is a fixed-size MongoDB collection that automatically overwrites its oldest documents once it reaches its allocated size, preserving natural insertion order without needing an index.
Capped collections are created with a maximum size (and optionally a maximum document count) up front. Once that limit is hit, MongoDB removes the oldest documents to make room for new ones in a circular-buffer fashion, so you never have to run manual cleanup jobs. Because insertion order is guaranteed and stored physically on disk in that order, capped collections support high-throughput inserts and efficient natural-order retrieval, which is why they are the backing store for the oplog and are popular for logging, caching, and other recent-data use cases. You cannot delete individual documents in a way that shrinks the size, and updates that grow a document's size are rejected.
- Automatic space reclamation without a TTL job or cron cleanup
- Guaranteed insertion order retrieval, ideal for logs and event streams
- High write throughput due to append-only, fixed-size storage
- Backs the replication oplog for tailable cursor reads
- No need to manage indexes just to keep recent-first ordering
AI Mentor Explanation
A capped collection is like a stadium scoreboard that only has room to display the last ten overs of ball-by-ball updates. As a new delivery is bowled, the oldest entry scrolls off the board automatically so the display never overflows. Nobody manually erases old overs; the board's fixed size does that job, and the most recent action is always what a fan glancing up actually sees first.
Step-by-Step Explanation
Step 1
Create with a size limit
Use db.createCollection('logs', { capped: true, size: 5242880 }) to allocate a fixed-size storage area in bytes, optionally with a max document count.
Step 2
Insert in natural order
Documents are physically stored in insertion order, so reads without a sort already come back oldest-to-newest.
Step 3
Automatic overwrite
Once the size (or max count) limit is reached, MongoDB removes the oldest documents to make space for new inserts, first-in-first-out.
Step 4
No shrinking updates or manual deletes
Updates cannot increase a document's size, and individual document deletes are disallowed to preserve the fixed layout.
Step 5
Tailable cursors for streaming
Open a tailable cursor to keep receiving newly inserted documents as they arrive, the same mechanism replication uses on the oplog.
What Interviewer Expects
- Explains that size is fixed and oldest documents get overwritten automatically
- Knows insertion order is preserved without a secondary index
- Mentions the oplog as a real-world capped collection example
- Understands documents cannot grow past their original size
- Can describe a suitable use case such as logging or caching recent events
Common Mistakes
- Thinking capped collections auto-expire by time like a TTL index
- Believing you can delete arbitrary documents from a capped collection
- Confusing capped collections with sharded collections
- Assuming updates can freely grow document size
Best Answer (HR Friendly)
“A capped collection is a special MongoDB collection with a fixed storage size that automatically discards its oldest data once full, similar to a rolling log. It is useful for things like activity logs or caches where you only care about the most recent entries and don't want to manage cleanup manually.”
Code Example
// Create a capped collection limited to 5MB and 1000 docs
db.createCollection('recentEvents', {
capped: true,
size: 5242880,
max: 1000
});
// Inserts preserve natural order automatically
db.recentEvents.insertOne({ event: 'login', userId: 42, ts: new Date() });
// Tail the collection like the oplog
const cursor = db.recentEvents.find().tailable(true).awaitData(true);
while (cursor.hasNext()) {
print(JSON.stringify(cursor.next()));
}Follow-up Questions
- How does a capped collection differ from a TTL index?
- Why does the oplog use a capped collection under the hood?
- Can you resize a capped collection after creation?
- What happens if you try to delete one document from a capped collection?
- When would you choose a capped collection over a regular collection with a sort?
MCQ Practice
1. What happens when a capped collection reaches its size limit?
Capped collections behave as a circular buffer, automatically overwriting the oldest documents once the fixed size is reached.
2. Which MongoDB internal feature is implemented as a capped collection?
The replication oplog is a capped collection, which is why tailable cursors were originally designed around it.
3. Which operation is NOT allowed on a capped collection?
Updates that would grow a document beyond its original allocated size are rejected to preserve the fixed on-disk layout.
Flash Cards
What is a capped collection? — A fixed-size MongoDB collection that automatically overwrites its oldest documents once full, like a circular buffer.
Does a capped collection need an index to preserve insertion order? — No, insertion order is preserved naturally by physical storage layout.
Name one real MongoDB feature built on a capped collection. — The replication oplog.
Can you delete a single document from a capped collection? — No, individual document deletion is not allowed; only the whole collection can be dropped.