Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

How to get document size inside aggregation

Hi All,

I have worked predominantly on relational databases and new to mongodb. Learning new things day by day. I am wondering how to get the size of the document inside the aggregation query?
db.collection.stats() gives me the total collection size. How to get a filtered dataset size

Thanks,
Vijay K

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 :wink:

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 :smiley:
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
    ]
  }
}

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

Thanks for the reply. Apologies for replying late. The aggregate function worked for my problem.