skip to content
Alvin Lucillo

Showing basic collection index stats

/ 1 min read

Let’s say we have the database index_size_repro and collection events and want to see the basic information that helps us compare index size against collection size. We can use stats().

size is the logical, uncompressed size of all documents in the collection, count is the total number of documents, storageSize is the storage allocated for the collection data, totalIndexSize is the storage allocated for all indexes, totalSize is the sum of storageSize and totalIndexSize, indexSizes lists each index and its individual size, avgObjSize is the average document size, ns is the namespace ([database].[collection]), and nindexes is the number of indexes.

By default, the size numbers are in bytes.

index_size_repro> const s = db.events.stats()

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: 35599998,
  count: 100000,
  storageSize: 29290496,
  totalIndexSize: 43667456,
  totalSize: 72957952,
  indexSizes: {
    _id_: 1306624,
    wideKey_1: 42360832
  },
  avgObjSize: 355,
  ns: 'index_size_repro.events',
  nindexes: 2
}