Bird
Raised Fist0
MongoDBquery~10 mins

Document size and growth patterns in MongoDB - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the size of a document in bytes.

MongoDB
db.collection.findOne({ _id: 1}).[1]()
Drag options to blanks, or click blank then click option'
Asize
Bbsonsize
CObject.bsonsize
DgetSize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' or 'getSize' which are not valid methods.
Trying to call bsonsize directly on the document.
2fill in blank
medium

Complete the code to calculate the average document size in a collection.

MongoDB
db.collection.aggregate([{ $group: { _id: null, avgSize: { $avg: { $[1]: "$${ROOT}" } } } }])
Drag options to blanks, or click blank then click option'
Alength
Bsize
CobjectSize
DbsonSize
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent operators like $size or $length in aggregation for document size.
Confusing field size with document size.
3fill in blank
hard

Fix the error in the code to correctly measure document size growth over time.

MongoDB
db.collection.find().forEach(doc => print(doc._id, Object.[1](doc)))
Drag options to blanks, or click blank then click option'
Absonsize
BbsonSize
Cbson_size
DbsonSizeOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect casing like 'bsonsize' or 'bson_size'.
Trying to call a non-existent method.
4fill in blank
hard

Fill both blanks to create a query that finds documents larger than 1KB.

MongoDB
db.collection.find({ $where: function() { return Object.[1](this) [2] 1024; } })
Drag options to blanks, or click blank then click option'
AbsonSize
B>
C<
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using 'size' instead of 'bsonSize' method.
5fill in blank
hard

Fill all three blanks to create a map-reduce that sums document sizes by category.

MongoDB
var map = function() { emit(this.[1], Object.[2](this)); };
var reduce = function(key, values) { return Array.[3](values, (a, b) => a + b, 0); };
db.collection.mapReduce(map, reduce, { out: 'sizeByCategory' });
Drag options to blanks, or click blank then click option'
Acategory
BbsonSize
Creduce
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field name or method names.
Using 'length' instead of 'reduce' for summing.

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

  1. Step 1: Understand document size impact

    Larger documents take more space and slow down database operations.
  2. Step 2: Connect size with performance and cost

    Smaller documents improve speed and reduce storage needs, saving costs.
  3. Final Answer:

    To improve database speed and reduce storage costs -> Option D
  4. 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

  1. Step 1: Identify command for collection stats

    The command db.collection.stats() returns detailed stats about the collection.
  2. Step 2: Confirm it includes document size info

    This command shows average document size, storage size, and growth info.
  3. Final Answer:

    db.collection.stats() -> Option A
  4. 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

  1. Step 1: Understand document growth effect

    As documents grow, their average size (avgObjSize) increases.
  2. Step 2: Connect growth to storage size

    More or bigger documents increase storageSize to hold data.
  3. Final Answer:

    Increasing avgObjSize and storageSize values -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify error cause

    db.collection.stats() requires an existing collection; misspelling prevents access.
  3. Final Answer:

    You misspelled the collection name -> Option B
  4. 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

  1. Step 1: Understand document growth challenges

    Unpredictable growth can cause documents to become too large and slow.
  2. Step 2: Apply best practice for size management

    Splitting large documents into smaller ones with references keeps sizes manageable and improves performance.
  3. Final Answer:

    Split large documents into smaller related documents using references -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard