Complete the code to find the size of a document in bytes.
db.collection.findOne({ _id: 1}).[1]()The Object.bsonsize() method returns the size of a document in bytes in MongoDB.
Complete the code to calculate the average document size in a collection.
db.collection.aggregate([{ $group: { _id: null, avgSize: { $avg: { $[1]: "$${ROOT}" } } } }])The $bsonSize operator calculates the BSON size of each document, which can be averaged.
Fix the error in the code to correctly measure document size growth over time.
db.collection.find().forEach(doc => print(doc._id, Object.[1](doc)))
The correct method name is Object.bsonSize() with a capital 'S'.
Fill both blanks to create a query that finds documents larger than 1KB.
db.collection.find({ $where: function() { return Object.[1](this) [2] 1024; } })Use Object.bsonSize(this) to get document size and compare if it's greater than 1024 bytes.
Fill all three blanks to create a map-reduce that sums document sizes by category.
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' });The map emits the category field and document size using Object.bsonSize(). The reduce uses Array.reduce() to sum sizes.