MongoDB stores data in documents. Knowing size limits and structure rules helps keep data safe and organized.
Document size limits and structure rules in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
No specific code syntax applies; these are rules about document size and structure.Each MongoDB document can be up to 16 megabytes (MB) in size.
Documents are stored as BSON, which supports nested objects and arrays.
Examples
MongoDB
{ "name": "Alice", "age": 30, "hobbies": ["reading", "hiking"] }MongoDB
{ "_id": ObjectId("507f1f77bcf86cd799439011"), "profile": { "bio": "...", "social": { "twitter": "@alice" } } }MongoDB
// Trying to insert a document larger than 16MB will cause an error.Sample Program
This inserts a normal small document successfully. Trying to insert a document larger than 16MB will fail.
MongoDB
db.users.insertOne({ "name": "Bob", "age": 25, "bio": "A short bio." })
// Then try to insert a very large document (simulated here as a comment)
// db.users.insertOne({ "largeField": new Array(17000000).join('a') })Important Notes
Always keep documents under 16MB to avoid errors.
Use embedded documents and arrays to organize data but avoid making documents too large.
For very large data, consider using GridFS or splitting data into multiple documents.
Summary
MongoDB documents have a maximum size of 16MB.
Documents can contain nested objects and arrays.
Keep documents small and well-structured for best performance.
Practice
1. What is the maximum size allowed for a single MongoDB document?
easy
Solution
Step 1: Recall MongoDB document size limit
MongoDB limits each document to a maximum size of 16MB to ensure efficient storage and retrieval.Step 2: Compare options with known limit
Only 16 megabytes matches the official 16MB limit; others are either too large or unlimited, which is incorrect.Final Answer:
16 megabytes -> Option BQuick Check:
MongoDB document max size = 16MB [OK]
Hint: Remember MongoDB docs max size is 16MB [OK]
Common Mistakes:
- Confusing document size with collection size
- Thinking documents can be unlimited in size
- Mixing up kilobytes and megabytes
2. Which of the following is a valid way to structure a MongoDB document?
easy
Solution
Step 1: Understand MongoDB document structure
MongoDB documents use key-value pairs with values as strings, numbers, arrays, or nested objects. Arrays are enclosed in square brackets [].Step 2: Evaluate each option's syntax
{ name: "Alice", age: 30, hobbies: ["reading", "hiking"] }correctly uses an array for hobbies. Options A and B list hobbies without array brackets, which is invalid.{ name: "Carol", age: 28, hobbies: { "reading", "hiking" } }uses curly braces for hobbies, which denotes an object, but the values are not key-value pairs, so it's invalid.Final Answer:
{ name: "Alice", age: 30, hobbies: ["reading", "hiking"] } -> Option AQuick Check:
Arrays use [] in MongoDB documents [OK]
Hint: Arrays in MongoDB use square brackets [] [OK]
Common Mistakes:
- Using curly braces {} for arrays
- Listing array items without brackets
- Confusing object and array syntax
3. Given the following MongoDB document, what is the size issue if any?
{ "name": "Eve", "data": "a".repeat(17000000) }medium
Solution
Step 1: Calculate approximate document size
The string "a" repeated 17,000,000 times is about 17MB, which exceeds MongoDB's 16MB document size limit.Step 2: Compare size with MongoDB limit
Since 17MB > 16MB, the document is too large and will cause an error on insert.Final Answer:
Document size exceeds 16MB limit -> Option AQuick Check:
17MB > 16MB limit = size error [OK]
Hint: Check if data size exceeds 16MB limit [OK]
Common Mistakes:
- Assuming large strings are allowed
- Ignoring size limits for large fields
- Confusing document size with field count
4. You try to insert this document but get an error:
What is the most likely cause?
{ "user": "John", "profile": { "age": 30, "hobbies": ["golf", "chess"] } }What is the most likely cause?
medium
Solution
Step 1: Check document structure validity
The document uses nested objects and arrays correctly, which MongoDB supports.Step 2: Consider size and syntax
The document is small and syntax is valid, so no size or syntax error should occur.Final Answer:
No error; document is valid and should insert successfully -> Option CQuick Check:
Nested objects and arrays are allowed [OK]
Hint: Nested objects and arrays are valid MongoDB document parts [OK]
Common Mistakes:
- Thinking nested objects are disallowed
- Assuming arrays must be flat
- Confusing size errors with syntax errors
5. You want to store a large list of 1 million numbers inside a single MongoDB document. What is the best approach to avoid exceeding document size limits?
hard
Solution
Step 1: Understand document size limits and large data
Storing 1 million numbers in one document likely exceeds the 16MB limit, causing errors.Step 2: Choose a strategy to handle large data
Splitting data into multiple smaller documents keeps each under the size limit and improves performance.Step 3: Evaluate other options
Storing as a large array or string risks size errors; nested objects won't reduce size significantly.Final Answer:
Split the list into multiple smaller documents -> Option DQuick Check:
Split large data to avoid 16MB limit [OK]
Hint: Split big data into smaller documents to fit size limits [OK]
Common Mistakes:
- Trying to store huge arrays in one document
- Converting arrays to strings without size benefit
- Assuming nested objects reduce document size
