Viewing database stats

In the previous recipe, we saw how to view some important statistics of a collection from an administrative perspective. In this recipe, we'll get an even clearer picture; getting those (or most of those) statistics at the database level.

Getting ready

To find the stats of the database, we need to have a server up and running, and a single node should be ok. Refer to the Single node installation of MongoDB recipe in Chapter 1, Installing and Starting the MongoDB Server, for how to start the server. The data on which we would be operating needs to be imported into the database. The steps to import the data are given in the Creating test data recipe in Chapter 2, Command-line Operations and Indexes. Once these steps are completed, we are all set to go ahead with this recipe. Refer to the previous recipe, Viewing collection stats, if you need to see how to view stats at the collection level.

How to do it…

We will be using the test database for the purpose of this recipe. It already has the postalCodes collection in it. Let's take a look at the steps in detail:

  1. Connect to the server using the Mongo shell by typing in the following command from the operating system terminal (it is assumed that the server is listening to port 27017):
    $ mongo
    
  2. On the shell, execute the following command and observe the output:
    > db.stats()
    
  3. Now, execute the following command, but this time with the scale parameter (observe the output):
    > db.stats(1024)
    {
      "db" : "test",
      "collections" : 3,
      "objects" : 39738,
      "avgObjSize" : 143.32699179626553,
      "dataSize" : 5562,
      "storageSize" : 16388,
      "numExtents" : 8,
      "indexes" : 2,
      "indexSize" : 2243,
      "fileSize" : 196608,
      "nsSizeMB" : 16,
      "dataFileVersion" : {
        "major" : 4,
        "minor" : 5
      },
      "ok" : 1
    }
    

How it works…

Let us start by looking at the collections field. If you look carefully at the number and also execute the show collections command on the Mongo shell, you will find one extra collection in the stats as compared to those achieved by executing the command. The difference is denotes one collection, which is hidden, and its name is system.namespaces. You may execute db.system.namespaces.find() to view its contents.

Getting back to the output of stats operation on the database, the objects field in the result has an interesting value too. If we find the count of documents in the postalCodes collection, we see that it is 39732. The count shown here is 39738, which means there are six more documents. These six documents come from the system.namespaces and system.indexes collection. Executing a count query on these two collections will confirm it. Note that the test database doesn't contain any other collection apart from postalCodes. The figures will change if the database contains more collections with documents in it.

The scale parameter, which is a parameter to the stats function, divides the number of bytes with the given scale value. In this case, it is 1024, and hence, all the values will be in KB. Let's analyze the output:

> db.stats(1024)
{
  "db" : "test",
  "collections" : 3,
  "objects" : 39738,
  "avgObjSize" : 143.32699179626553,
  "dataSize" : 5562,
  "storageSize" : 16388,
  "numExtents" : 8,
  "indexes" : 2,
  "indexSize" : 2243,
  "fileSize" : 196608,
  "nsSizeMB" : 16,
  "dataFileVersion" : {
    "major" : 4,
    "minor" : 5
  },
  "ok" : 1
}

The following table shows the meaning of the important fields:

Field

Description

db

This is the name of the database whose stats are being viewed.

collections

This is the total number of collections in the database.

objects

This is the count of documents across all collections in the database. If we find the stats of a collection by executing db.<collection>.stats(), we get the count of documents in the collection. This attribute is the sum of counts of all the collections in the database.

avgObjectSize

This is simply the size (in bytes) of all the objects in all the collections in the database, divided by the count of the documents across all the collections. This value is not affected by the scale provided even though this is a size field.

dataSize

This is the total size of the data held across all the collections in the database. This value is affected by the scale provided.

storageSize

This is the total amount of storage allocated to collections in this database for storing documents. This value is affected by the scale provided.

numExtents

This is the count of all the extents in the database across all the collections. This is basically the sum of numExtents in the collection stats for collections in this database.

indexes

This is the sum of indexes across all collections in the database.

indexSize

This is the size (in bytes) for all the indexes of all the collections in the database. This value is affected by the scale provided.

fileSize

This is simply the addition of the size of all the database files you should find on the filesystem for this database. The files will be named test.0, test.1, and so on for the test database. This value is affected by the scale provided.

nsSizeMB

This is the size of the file in MBs for the .ns file of the database.

Another thing to note is the value of avgObjectSize, and there is something weird in this value. Unlike this very field in the collection's stats, which is affected by the value of the scale provided, in database stats this value is always in bytes, which is pretty confusing and one cannot really be sure why this is not scaled according to the provided scale.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset