skip to content
Alvin Lucillo

Show stats sizes in MB

/ 1 min read

By default, db.collection.stats() shows sizes in bytes, which may be hard to read for some. To change that to MB, use {scale: 1024 * 1024}. This means the numbers will be scaled down to megabyte values. Since 1 KB = 1024 bytes and 1 MB = 1024 KB, bytes are converted into megabytes by dividing by the product of the two numbers (i.e., 1,048,576).

Below, that would mean totalSize is 69.578125 MB

index_size_repro> const s = db.events.stats({scale: 1024 * 1024})

index_size_repro> printjson({ size: s.size, count: s.count, storageSize: s.storageSize, totalIndexSize: s.totalIndexSize, totalSize: s.totalSize, indexSizes: s.indexSizes, avgObjSize: s.avgObjSize, ns: s.ns, nindexes: s.nindexes })
{
  size: 33.95080375671387,
  count: 100000,
  storageSize: 27.93359375,
  totalIndexSize: 41.64453125,
  totalSize: 69.578125,
  indexSizes: {
    _id_: 1.24609375,
    wideKey_1: 40.3984375
  },
  avgObjSize: 355,
  ns: 'index_size_repro.events',
  nindexes: 2
}