Computer scienceFundamentalsSQL and DatabasesDBMSMongoDBOperations and operators

Database statistics

4 minutes read

Sometimes you might need to get detailed information about your database or specific collection in MongoDB. Statistics can be useful in a lot of cases: while scaling, planning, or just observing the database. So, the stats() function comes in handy in situations like these. In this topic, you will learn how to view statistics in the MongoDB database management system.

Database statistics

So, if you want to get the statistics of the whole database, you can use the db.stats() command. It can take two optional arguments:

  • scale (number) — the size of the scaling factor of the data. The default value is 1, so all data is represented in bytes. For example, to display kilobytes, the value of 1024 is used.
  • freeStorage (number) — the value of 1 is used to display information about free space allocated to collections.

Function arguments are specified in JSON format like this:

db.stats({scale: 1024, freeStorage: 1})

Take a look at the example of using this command (without any arguments) with some database:

{
  db: 'database',
  collections: 5,
  views: 0,
  objects: 1689,
  avgObjSize: 56.56542332741267,
  dataSize: 73.702145454375,
  storageSize: 100,
  totalFreeStorageSize: 32,
  numExtents: Long("0"),
  indexes: 2,
  indexSize: 116,
  totalSize: 216,
  totalFreeStorageSize: 73,
  indexFreeStorageSize: 36,
  fileSize: 60155820,
  nsSizeMB: 17,
  ok: 1
}

It's a lot of information, right? Let's go through some of the main lines of this output.

  • db — the name of your database. In our case, the name is just 'database'.
  • collections — the number of collections a database has.
  • objects — the number of objects (documents) in the database.
  • avgObjSize — the average size of each document.
  • dataSize — total size of the whole database.
  • storageSize — space allocated to the collections in the database.
  • indexes — the number of indexes in the database.
  • indexSize — space allocated to the indexes in the database.
  • totalSize — the sum of storageSize and indexSize.

You can also get a specific value that you want. You only need to specify the value after the stats() command. For example, if you want to get just the number of objects in the database, use this:

db.stats().objects

And it will return only the number 1689.

Hopefully, now you understand what information the statistics about the database include.

Collection statistics

In the previous paragraph, we've talked about the statistics of the whole database including all its collections. In case we want to see the statistics of a specific collection, we need to use db.<collection_name>.stats(). The function also takes some optional arguments:

  • scale (number) — the size of the scaling factor of the data. The same as with the database statistics.
  • indexDetails (boolean) — if true, the index details will be output to the stats.
  • indexDetailsKey (document) — by giving the index key specification, you may utilize indexDetailsKey to filter index details.
  • indexDetailsName (string) — by providing the index name, you can filter index information using this function.

The example output without optional arguments may look like this:

{
  ns: 'database.collecion',
  size: 137,
  count: 2,
  avgObjSize: 68,
  storageSize: 20480,
  freeStorageSize: 0,
  capped: false,
  nindexes: 1,
  indexBuilds: [],
  totalIndexSize: 20480,
  totalSize: 40960,
  indexSizes: { _id_: 20480 },
  scaleFactor: 1,
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1665833219, i: 25 }),
    signature: {
      hash: Binary(Buffer.from("5079ed13a614754e6e786d3bb8be02431696dfdd", "hex"), 0),
      keyId: Long("7098383576420319266")
    }
  },
  operationTime: Timestamp({ t: 1665833219, i: 25 })
}

The field names here mean the same thing as in the case of the statistics of the entire database. You can also get specific values as shown in the previous paragraph. Arguments are also specified in JSON:

db.collecion.stats({scale: 1024, 
                    indexDetails: true, 
                    indexDetailsName: 'name'})

Conclusion

In this topic, you've studied how you can view the statistics of both the entire database and one collection in MongoDB. This can be useful when you have a lot of different data stored and you want to know a little more information about how exactly it's stored and how much space it takes up.

12 learners liked this piece of theory. 1 didn't like it. What about you?
Report a typo