Bird
Raised Fist0
MongoDBquery~20 mins

Document size limits and structure rules in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
MongoDB Document Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:00remaining
What is the maximum size of a single MongoDB document?
MongoDB has a limit on how large a single document can be. What is the maximum size allowed for one document?
A1 gigabyte (GB)
B64 megabytes (MB)
C16 megabytes (MB)
D256 kilobytes (KB)
Attempts:
2 left
💡 Hint
Think about the BSON document size limit.
query_result
intermediate
1:30remaining
What happens if you try to insert a document larger than the limit?
You try to insert a document with size 20 MB into a MongoDB collection. What will be the result?
AThe insert operation fails with an error about document size.
BThe document is automatically split into smaller documents.
CThe document is truncated to 16 MB and inserted.
DThe insert succeeds without any error.
Attempts:
2 left
💡 Hint
MongoDB enforces strict size limits on documents.
📝 Syntax
advanced
2:00remaining
Which document structure violates MongoDB rules?
Consider these four MongoDB documents. Which one violates MongoDB's document structure rules?
A{ "name": "Carol", "age": 27, "address": { "street": "Main St", "city": "Town" } }
B{ "name": "Bob", "age": 25, "$invalidField": "value" }
C{ "name": "Alice", "age": 30, "_id": ObjectId("507f1f77bcf86cd799439011") }
D{ "name": "Dave", "age": 22, "hobbies": ["reading", "sports"] }
Attempts:
2 left
💡 Hint
Field names starting with $ are reserved in MongoDB.
optimization
advanced
2:00remaining
How to store large files exceeding document size limit in MongoDB?
You need to store files larger than 16 MB in MongoDB. Which approach is correct?
AUse GridFS to split and store files in chunks.
BCompress the file and store it as a string in one document.
CStore the entire file as a single document with a large binary field.
DSplit the file manually into multiple documents without using any special feature.
Attempts:
2 left
💡 Hint
MongoDB provides a special specification for large files.
🔧 Debug
expert
2:30remaining
Why does this MongoDB insert fail with a 'BSONObjectTooLarge' error?
You run this insert command:
db.collection.insertOne({ "data": new Array(17000000).fill(0) })

It fails with a 'BSONObjectTooLarge' error. Why?
AThe 'fill' method is not supported in MongoDB shell.
BMongoDB does not allow arrays as field values.
CThe insertOne method requires a smaller batch size parameter.
DThe array creates a document larger than 16 MB, exceeding the limit.
Attempts:
2 left
💡 Hint
Think about the size of the document created by the array.

Practice

(1/5)
1. What is the maximum size allowed for a single MongoDB document?
easy
A. 512 kilobytes
B. 16 megabytes
C. 1 gigabyte
D. Unlimited size

Solution

  1. Step 1: Recall MongoDB document size limit

    MongoDB limits each document to a maximum size of 16MB to ensure efficient storage and retrieval.
  2. 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.
  3. Final Answer:

    16 megabytes -> Option B
  4. Quick 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
A. { name: "Alice", age: 30, hobbies: ["reading", "hiking"] }
B. { name: "Bob", age: 25, hobbies: "reading", "hiking" }
C. { name: "Carol", age: 28, hobbies: { "reading", "hiking" } }
D. { name: "Dave", age: 40, hobbies: reading, hiking }

Solution

  1. 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 [].
  2. 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.
  3. Final Answer:

    { name: "Alice", age: 30, hobbies: ["reading", "hiking"] } -> Option A
  4. Quick 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
A. Document size exceeds 16MB limit
B. Document is valid and within size limits
C. Document has invalid syntax
D. Document size is less than 1MB

Solution

  1. 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.
  2. Step 2: Compare size with MongoDB limit

    Since 17MB > 16MB, the document is too large and will cause an error on insert.
  3. Final Answer:

    Document size exceeds 16MB limit -> Option A
  4. Quick 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:
{ "user": "John", "profile": { "age": 30, "hobbies": ["golf", "chess"] } }

What is the most likely cause?
medium
A. Document exceeds 16MB size limit
B. Nested objects are not allowed in MongoDB documents
C. No error; document is valid and should insert successfully
D. Array syntax is incorrect

Solution

  1. Step 1: Check document structure validity

    The document uses nested objects and arrays correctly, which MongoDB supports.
  2. Step 2: Consider size and syntax

    The document is small and syntax is valid, so no size or syntax error should occur.
  3. Final Answer:

    No error; document is valid and should insert successfully -> Option C
  4. Quick 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
A. Use nested objects to store the list in one document
B. Store the entire list as a single large array in one document
C. Convert the list to a string and store it in one field
D. Split the list into multiple smaller documents

Solution

  1. Step 1: Understand document size limits and large data

    Storing 1 million numbers in one document likely exceeds the 16MB limit, causing errors.
  2. Step 2: Choose a strategy to handle large data

    Splitting data into multiple smaller documents keeps each under the size limit and improves performance.
  3. Step 3: Evaluate other options

    Storing as a large array or string risks size errors; nested objects won't reduce size significantly.
  4. Final Answer:

    Split the list into multiple smaller documents -> Option D
  5. Quick 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