Capped collections for fixed-size data in MongoDB - Time & Space Complexity
When working with capped collections in MongoDB, it's important to understand how the time to insert or read data changes as the collection grows.
We want to see how the fixed size of capped collections affects the speed of operations.
Analyze the time complexity of inserting documents into a capped collection.
db.createCollection("logs", { capped: true, size: 100000 });
for (let i = 0; i < n; i++) {
db.logs.insertOne({ message: `Log entry ${i}`, timestamp: new Date() });
}
This code creates a capped collection with a fixed size and inserts n log entries into it.
Look at what repeats as we insert documents.
- Primary operation: Inserting one document into the capped collection.
- How many times: n times, once for each log entry.
As we add more entries, the collection stays the same size, so old entries get removed automatically.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 insert operations, each quick |
| 100 | 100 insert operations, still quick |
| 1000 | 1000 insert operations, speed stays steady |
Pattern observation: Each insert takes about the same time regardless of how many documents are already in the capped collection.
Time Complexity: O(n)
This means inserting n documents takes time proportional to n, with each insert operation being very fast and consistent.
[X] Wrong: "Inserting into a capped collection slows down as it fills up because it has to delete old data."
[OK] Correct: The capped collection is fixed size and uses a circular buffer, so insert speed stays constant no matter how full it is.
Understanding capped collections shows you know how fixed-size data storage works efficiently, a useful skill for real-world logging and data retention tasks.
"What if we changed the capped collection to a normal collection without size limits? How would the time complexity of inserts change as data grows?"
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
