You can use Object.bsonsize<object> to get object size in bytes.
// this will work in the mongo shell only
const doc = db.coll.findOne(<queryFilter>);
Object.bsonsize(doc);
It is possible to get the object size in the Node.js environment using bson library. There should be similar drivers for other programming languages as well in web
const bson = require('bson');
const doc = await db.coll.findOne(<queryFilter>);
const size = bson.calculateObjectSize(doc);
Within aggregation you can get average document size in the collection, like this:
db.coll.aggregate([
{
$collStats: {
storageStats: { },
},
},
{
$project: {
averageDocumentSize: '$storageStats.avgObjSize',
},
},
]);
You can also, use map-reduce to take advantage of Object.bsonsize
Like this:
const mapFn1 = function() {
emit(this.groupId, this);
};
const reduceFn1 = function(groupId, documents) {
const sizes = documents.map((item) => {
return {
docId: item._id,
size: Object.bsonsize(item),
};
});
return { result: sizes };
};
db.coll.mapReduce(
mapFn1,
reduceFn1,
{ out: 'mr_out' },
)
This will output to ‘mr_out’ collection something similar to this:
{
"_id" : null,
"value" : {
"result" : [
{
"docId" : ObjectId("5ef0c89b8ce9f870270090e1"),
"size" : 241
},
{
"docId" : ObjectId("5ef0c89b8ce9f870270090e2"),
"size" : 76
},
// ... other docs in the collection
]
}
}