📖 Scenario: You are managing a MongoDB collection that stores user profiles for a social media app. Each user document contains basic information and an array of posts. You want to understand how document size grows as users add more posts.
🎯 Goal: Build a MongoDB document structure for user profiles, add a configuration for maximum posts allowed, insert posts dynamically, and finally update the document to reflect the growth pattern.
📋 What You'll Learn
Create a MongoDB document for a user with specific fields
Add a configuration variable for maximum posts allowed
Insert posts into the user's posts array using an update operation
Update the document to reflect the final state with all posts
💡 Why This Matters
🌍 Real World
Social media apps and content management systems often store user-generated content in documents that grow over time. Understanding document size helps optimize performance and storage.
💼 Career
Database administrators and backend developers need to manage document growth to maintain efficient queries and avoid exceeding size limits in MongoDB.
Progress0 / 4 steps
1
Create the initial user document
Create a MongoDB document called userProfile with these exact fields: _id set to 1, name set to "Alice", and an empty array posts.
MongoDB
Hint
Use a JavaScript object with keys _id, name, and posts initialized as an empty array.
2
Add a maximum posts configuration
Create a variable called maxPosts and set it to 3 to limit the number of posts a user can have.
MongoDB
Hint
Just create a simple variable maxPosts and assign the number 3.
3
Add posts to the user document
Use a for loop with variable i from 1 to maxPosts inclusive to push new post objects into userProfile.posts. Each post object should have postId set to i and content set to `Post number ${i}` using template strings.
MongoDB
Hint
Use a for loop from 1 to maxPosts and push objects with postId and content into the posts array.
4
Update the user document to reflect growth
Add a new field postCount to userProfile and set it to the length of the posts array using userProfile.posts.length.
MongoDB
Hint
Assign userProfile.postCount to the length of the posts array.
Practice
(1/5)
1. What is the main reason to keep MongoDB documents reasonably small?
easy
A. To make documents harder to read
B. To avoid using indexes
C. To increase the number of collections
D. To improve database speed and reduce storage costs
Solution
Step 1: Understand document size impact
Larger documents take more space and slow down database operations.
Step 2: Connect size with performance and cost
Smaller documents improve speed and reduce storage needs, saving costs.
Final Answer:
To improve database speed and reduce storage costs -> Option D
Quick Check:
Small documents = faster and cheaper [OK]
Hint: Smaller documents mean faster queries and less storage [OK]
Common Mistakes:
Thinking bigger documents are always better
Confusing collections with document size
Ignoring storage cost impact
2. Which MongoDB command shows statistics including average document size and storage size?
easy
A. db.collection.stats()
B. db.collection.insert()
C. db.collection.find()
D. db.collection.update()
Solution
Step 1: Identify command for collection stats
The command db.collection.stats() returns detailed stats about the collection.
Step 2: Confirm it includes document size info
This command shows average document size, storage size, and growth info.
Final Answer:
db.collection.stats() -> Option A
Quick Check:
Stats command = db.collection.stats() [OK]
Hint: Use .stats() to check collection size and growth info [OK]
Common Mistakes:
Using find() which only queries documents
Confusing insert() or update() with stats
Not knowing stats() returns size info
3. Given a collection with documents growing in size over time, what will db.collection.stats() likely show?
medium
A. Increasing avgObjSize and storageSize values
B. Decreasing avgObjSize and storageSize values
C. No change in avgObjSize but increasing storageSize
D. Only the number of documents changes
Solution
Step 1: Understand document growth effect
As documents grow, their average size (avgObjSize) increases.
Step 2: Connect growth to storage size
More or bigger documents increase storageSize to hold data.
Final Answer:
Increasing avgObjSize and storageSize values -> Option A
Quick Check:
Growth means bigger avgObjSize and storageSize [OK]
Hint: Growth means both average document and storage sizes rise [OK]
Common Mistakes:
Assuming storage size stays constant
Thinking avgObjSize decreases with growth
Ignoring storage size changes
4. You run db.collection.stats() but get an error. Which is a likely cause?
medium
A. You used find() instead of stats()
B. You misspelled the collection name
C. You forgot to connect to MongoDB
D. You ran stats() on a database instead of a collection
Solution
Step 1: Check command usage context
A common error is misspelling the collection name, causing 'ns not found' error since the collection doesn't exist.
Step 2: Identify error cause
db.collection.stats() requires an existing collection; misspelling prevents access.
Final Answer:
You misspelled the collection name -> Option B
Quick Check:
stats() works on existing collections only [OK]
Hint: Verify collection name spelling before running stats() [OK]
Common Mistakes:
Misspelling the collection name
Forgetting to connect to MongoDB
Confusing find() with stats()
5. You have a collection where documents grow unpredictably. What is a good strategy to manage document size and growth?
hard
A. Ignore document size and rely on hardware upgrades
B. Keep all data in one large document to avoid joins
C. Split large documents into smaller related documents using references
D. Delete old documents frequently to reduce size
Solution
Step 1: Understand document growth challenges
Unpredictable growth can cause documents to become too large and slow.
Step 2: Apply best practice for size management
Splitting large documents into smaller ones with references keeps sizes manageable and improves performance.
Final Answer:
Split large documents into smaller related documents using references -> Option C
Quick Check:
Split documents to manage size and growth [OK]
Hint: Use references to split big documents for better growth control [OK]
Common Mistakes:
Keeping all data in one big document
Ignoring size issues hoping hardware helps
Deleting data instead of managing structure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.