Challenge - 5 Problems
Document Size Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate average document size in a collection
Given a MongoDB collection named orders, which aggregation pipeline will correctly calculate the average size in bytes of documents stored in this collection?
Attempts:
2 left
💡 Hint
Use $bsonSize to get the size of each document in bytes before averaging.
✗ Incorrect
Option C first projects the size of each document using $bsonSize, then groups all documents to calculate the average size. Option C tries to use $bsonSize inside $avg directly, which is not allowed. Option C uses $strLenCP which measures string length, not document size. Option C counts documents but does not calculate size.
🧠 Conceptual
intermediate1:30remaining
Understanding document growth and padding
In MongoDB, when a document grows in size due to updates, what mechanism helps reduce the need for document moves and fragmentation?
Attempts:
2 left
💡 Hint
Think about how MongoDB avoids moving documents when they grow slightly.
✗ Incorrect
MongoDB pre-allocates extra space (padding) around documents to allow them to grow without moving. This reduces fragmentation and improves update performance. Compression and indexes do not directly affect document growth handling.
📝 Syntax
advanced2:00remaining
Identify the correct syntax to find documents larger than 16MB
Which MongoDB query correctly finds documents in the files collection whose BSON size exceeds 16 megabytes (16 * 1024 * 1024 bytes)?
Attempts:
2 left
💡 Hint
Use $expr to compare computed values in queries.
✗ Incorrect
Option B uses $expr with $bsonSize to compute document size and compare it to 16MB correctly. Option B tries to use $bsonSize as a query operator which is invalid. Option B queries a non-existent field 'size'. Option B uses aggregation syntax incorrectly inside find.
🔧 Debug
advanced1:30remaining
Why does this update cause document move despite padding?
A document in MongoDB has padding to allow growth. However, after an update that adds a large array, the document moves to a new location. What is the most likely reason?
Attempts:
2 left
💡 Hint
Consider the size limits of padding space.
✗ Incorrect
Padding space is limited. If the document grows beyond this space, MongoDB must move it to a new location. Indexes do not prevent moves. MongoDB supports moves when necessary. The update operator used does not affect move necessity.
❓ optimization
expert2:30remaining
Optimizing document growth to reduce fragmentation
You have a collection where documents frequently grow due to added fields and arrays. Which approach best reduces document moves and fragmentation over time?
Attempts:
2 left
💡 Hint
Think about how to prepare documents for growth in advance.
✗ Incorrect
Pre-allocating fields or arrays with empty values reserves space inside documents, reducing moves when data is added. Smaller documents and splitting data help but do not directly reduce moves. Disabling padding increases moves. Capped collections have fixed size and do not allow growth.