0
0
MongoDBquery~10 mins

Capped collections for fixed-size data in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Capped collections for fixed-size data
Create capped collection with size limit
Insert documents
Check if collection size exceeds limit?
NoContinue inserting
Yes
Automatically remove oldest documents
Collection size stays within fixed limit
This flow shows how MongoDB capped collections keep data size fixed by automatically removing oldest documents when size limit is reached.
Execution Sample
MongoDB
db.createCollection("logs", { capped: true, size: 1000 })
db.logs.insert({ event: "start" })
db.logs.insert({ event: "process" })
db.logs.insert({ event: "end" })
Create a capped collection named 'logs' with max size 1000 bytes and insert three event documents.
Execution Table
StepActionCollection Size (bytes)Documents in CollectionNotes
1Create capped collection 'logs' with size 10000[]Empty collection created
2Insert { event: 'start' }approx 50[{ event: 'start' }]First document added
3Insert { event: 'process' }approx 100[{ event: 'start' }, { event: 'process' }]Second document added
4Insert { event: 'end' }approx 150[{ event: 'start' }, { event: 'process' }, { event: 'end' }]Third document added
5Insert large documents repeatedly1000 (max)[oldest documents removed automatically]Oldest docs removed to keep size <= 1000
6Try to insert document exceeding size1000[documents fitting size]Insert fails if document too large
7Stop inserting<=1000[documents within size limit]Collection size fixed by capped limit
💡 Execution stops because capped collection size limit is enforced, oldest documents removed automatically to keep size fixed.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Collection Size (bytes)05010015010001000
Documents in Collection[][{ event: 'start' }][{ event: 'start' }, { event: 'process' }][{ event: 'start' }, { event: 'process' }, { event: 'end' }][oldest removed to keep size][documents within size limit]
Key Moments - 2 Insights
Why does the collection remove oldest documents automatically?
Because the collection is capped with a fixed size (see execution_table step 5), MongoDB removes oldest documents to keep the total size within the limit.
Can a document larger than the capped size be inserted?
No, as shown in execution_table step 6, if a document is larger than the capped size, the insert will fail because it cannot fit.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, how many documents are in the collection?
A3
B1
C2
D4
💡 Hint
Check the 'Documents in Collection' column at step 4 in the execution_table.
At which step does the collection size reach its maximum capped size?
AStep 6
BStep 3
CStep 5
DStep 7
💡 Hint
Look at the 'Collection Size (bytes)' column to see when it reaches 1000.
If you try to insert a document larger than the capped size, what happens?
AOldest documents are removed to fit it
BThe insert fails
CThe collection size increases beyond the cap
DThe document is split into smaller parts
💡 Hint
Refer to step 6 in the execution_table where large document insert is attempted.
Concept Snapshot
Capped collections store documents with a fixed max size.
When size limit is reached, oldest documents are removed automatically.
Insert fails if a single document is larger than the capped size.
Use db.createCollection with { capped: true, size: <bytes> } to create.
Ideal for logs or fixed-size queues where order and size matter.
Full Transcript
This visual execution shows how MongoDB capped collections work. First, a capped collection is created with a fixed size limit. Documents are inserted one by one, increasing the collection size. When the size limit is reached, MongoDB automatically removes the oldest documents to keep the collection size within the limit. If a document is too large to fit, the insert fails. This behavior ensures the collection size stays fixed, useful for logs or queues.