What if your database could clean itself up and never run out of space?
Why Capped collections for fixed-size data in MongoDB? - Purpose & Use Cases
Imagine you have a small notebook where you write down your daily expenses. When the notebook is full, you have to erase old pages manually to make space for new notes.
Manually erasing old pages is slow and you might accidentally erase important notes. Also, keeping track of what to erase wastes time and can cause mistakes.
Capped collections automatically keep only the newest data up to a fixed size. Old data is removed without any extra work, so your storage never overflows and you always have recent information.
db.expenses.insert({date: '2024-06-01', amount: 20}); // Manually delete old entries when fulldb.createCollection('expenses', {capped: true, size: 10000}); // Auto-remove old data when size limit reached
You can store continuous streams of data without worrying about running out of space or manual cleanup.
A chat app uses capped collections to keep only the latest 1000 messages, ensuring fast access and fixed storage size.
Capped collections limit data size automatically.
They remove oldest data when full, no manual cleanup needed.
Perfect for logs, caches, or real-time data streams.
Practice
capped collection in MongoDB?Solution
Step 1: Understand capped collection purpose
Capped collections are designed to hold a fixed amount of data, automatically removing the oldest entries when full.Step 2: Compare options with this behavior
Only To store data with a fixed size limit and automatically remove old entries describes fixed size and automatic removal of old data, matching capped collection behavior.Final Answer:
To store data with a fixed size limit and automatically remove old entries -> Option CQuick Check:
Capped collection = fixed size + auto-remove old [OK]
- Thinking capped collections grow unlimited
- Confusing capped with in-memory storage
- Assuming capped collections support complex transactions
logs with a maximum size of 1MB?Solution
Step 1: Recall syntax for capped collection creation
The correct syntax usesdb.createCollectionwith options includingcapped: trueandsizein bytes.Step 2: Check each option
db.createCollection('logs', { capped: true, size: 1048576 }) matches the correct syntax. db.logs.create({ capped: true, size: 1048576 }) uses wrong method. db.createCollection('logs', { size: 1048576 }) missescapped: true. db.createCappedCollection('logs', 1048576) is invalid.Final Answer:
db.createCollection('logs', { capped: true, size: 1048576 }) -> Option DQuick Check:
Create capped = createCollection + capped:true + size [OK]
- Omitting capped:true option
- Using wrong method like createCappedCollection
- Specifying size without capped:true
cache with size 100 bytes, what will be the result of inserting 3 documents each of size 50 bytes?Solution
Step 1: Understand capped collection size behavior
The collection size is 100 bytes. Each document is 50 bytes, so two documents fit exactly.Step 2: Analyze insertion of 3 documents
When inserting the third document, the oldest document (first) is removed to keep size within 100 bytes, so last two remain.Final Answer:
Only the last 2 documents will be stored, the first is removed -> Option AQuick Check:
Size limit causes oldest removal [OK]
- Assuming all documents fit ignoring size
- Expecting insertion failure instead of removal
- Thinking only first document stays
db.createCollection('events', { capped: true }) but got an error. What is the likely cause?Solution
Step 1: Recall required options for capped collections
When creating a capped collection, thesizeoption (max bytes) is mandatory.Step 2: Analyze the error cause
Omittingsizecauses an error. Collection name and method are valid.maxis optional.Final Answer:
Missing the requiredsizeoption specifying the maximum bytes -> Option BQuick Check:
Capped creation requires size option [OK]
- Forgetting the size option
- Thinking collection names are restricted
- Confusing max documents with required size
userLogs with a max of 1000 documents and size limit 5MB. Which command correctly creates this collection?Solution
Step 1: Identify required options for capped collection with limits
Must specifycapped: true,sizein bytes, andmaxdocuments.Step 2: Evaluate each option
db.createCollection('userLogs', { capped: true, size: 5242880, max: 1000 }) correctly includes all three: capped true, size 5MB (5*1024*1024=5242880 bytes), and max 1000. Others miss required options or have wrong values.Final Answer:
db.createCollection('userLogs', { capped: true, size: 5242880, max: 1000 }) -> Option AQuick Check:
Capped + size in bytes + max docs = correct creation [OK]
- Omitting size or max options
- Using size as number of documents
- Not setting capped:true
Ready to prove it?
Pass the test and this pattern moves unknown
