0
0
MongoDBquery~10 mins

Document size and growth patterns in MongoDB - Interactive Code Practice

Choose your learning style9 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.