How can I calculate the disk space (bytes) used by documents returned from a mongoose request?

The scenario is as follows:

  • using Mongoose.js in Node.js
  • execute a request, e.g. Model.find().where('something', 'value').exec(callback)
  • you get an array of documents in callback

How to determine the size of these documents on disk?

The approximations are fine, at least for me.

+6
source share
1 answer

You can do this by executing your request as a post request (to get simple JavaScript objects instead of instances of a Mongoose document), and then plunge into the BSON library, which is part of the MongoDB native driver and calls calculateObjectSize :

 var bson = mongoose.mongo.BSON; Model.find().where('something', 'value').lean().exec(function(err, docs) { var docsBsonSize = bson.calculateObjectSize(docs)); }); 

It will not give you size on disk, but it should be a good approximation to the size of BSON documents.

+5
source

Source: https://habr.com/ru/post/981114/


All Articles