What is capped collection in MongoDB and when is it useful?
Learn what a MongoDB capped collection is, how its fixed-size FIFO circular buffer auto-evicts old data, and when to use it for logs and recent-event feeds.
Expected Interview Answer
A capped collection is a fixed-size MongoDB collection that behaves like a circular buffer: once it reaches its size (or optional document-count) limit, the oldest documents are automatically overwritten by new inserts, preserving insertion order.
Capped collections have high-throughput inserts because documents are stored in insertion order and never move, and reads in natural order are very fast. They forbid operations that would grow a document beyond its original size and do not allow deleting individual documents. They are ideal for rolling logs, recent-event caches, and other fixed-window data where you only care about the newest records.
- Automatic FIFO eviction of oldest data
- Very fast inserts and natural-order reads
- Bounded, predictable storage footprint
- Preserves insertion order without an index
- Great fit for rolling logs and recent-activity feeds
AI Mentor Explanation
A capped collection is like the last-ten-overs panel on a broadcast that always shows exactly ten overs. When the eleventh-most-recent over arrives, the oldest scrolls off automatically — you never manage deletions, the window just slides forward. It keeps only the most recent action in a fixed frame, exactly as a capped collection keeps the newest inserts and drops the oldest.
Step-by-Step Explanation
Step 1
Create with a size cap
Use createCollection with capped: true and a size in bytes; optionally add a max document count.
Step 2
Insert as usual
Documents are appended in insertion order and physically stored contiguously for fast writes.
Step 3
Automatic eviction
When the size or count limit is hit, MongoDB overwrites the oldest documents to make room.
Step 4
Read in natural order
Query without a sort to get insertion order, or use $natural: -1 for newest-first, both very fast.
Step 5
Respect the constraints
You cannot delete single documents or grow a document beyond its original size; drop the whole collection to clear it.
What Interviewer Expects
- Definition as a fixed-size circular buffer with FIFO eviction
- Knowledge that insertion order is preserved
- Awareness of operational limits (no single deletes, no doc growth)
- Realistic use cases like logging and recent-event feeds
- How to create one with size and max options
Common Mistakes
- Thinking you can delete individual documents from a capped collection
- Trying to update a document to a larger size
- Confusing the size cap (bytes) with the optional max (count)
- Using it for data that must be retained permanently
- Assuming you can convert it back to uncapped in place
Best Answer (HR Friendly)
“A capped collection is a MongoDB collection with a fixed maximum size. Once it fills up, the oldest records are automatically replaced by new ones, like a rolling log that never grows beyond a set limit. It is handy for things like recent activity feeds or logs where only the newest data matters.”
Code Example
// Create a 1 MB capped collection holding at most 5000 docs
db.createCollection('recentLogs', {
capped: true,
size: 1048576, // max bytes (required)
max: 5000 // optional max document count
})
// Inserts append in order; oldest are auto-evicted when full
db.recentLogs.insertOne({ ts: new Date(), level: 'info', msg: 'started' })
// Newest-first read using natural order (no index needed)
db.recentLogs.find().sort({ $natural: -1 }).limit(10)
// Confirm it is capped
db.recentLogs.isCapped() // trueFollow-up Questions
- How does a capped collection differ from a TTL index for expiring data?
- Can you shard a capped collection?
- What happens if you try to update a document to be larger in a capped collection?
- Why are tailable cursors often used with capped collections?
- How do you clear or resize a capped collection?
MCQ Practice
1. What happens when a capped collection reaches its size limit?
A capped collection is a circular buffer; new inserts evict the oldest documents once the cap is reached.
2. Which operation is NOT allowed on a capped collection?
You cannot delete individual documents from a capped collection; only dropping the whole collection removes data.
3. A capped collection is best suited for:
Its fixed-size FIFO behavior fits rolling logs and recent activity where only the newest records matter.
Flash Cards
What is a capped collection? — A fixed-size collection that acts as a circular buffer, auto-evicting the oldest documents when full.
What order do capped collections preserve? — Insertion (natural) order, without needing an index.
What can't you do in a capped collection? — Delete single documents or grow a document beyond its original size.
A common use case? — Rolling logs, recent-event caches, and feeds where only the newest data matters.