Performance monitoring
Determining what is considered normal for your database requires gathering statistics over a period of time. The period of time varies widely, depending upon the frequency and volume of database usage. In this section, we examine two primary means of gathering statistics: the db*stats() and db.serverStatus() methods. First, we have a look at how to monitor MongoDB performance using built-in mongo shell methods that produce statistics.
Mongo shell stats() methods
The most readily available monitoring command is the stats() shell method, available at both the database and collection levels. At the database level, this shell method gives important information such as the number of collections and indexes, as well as information on the average document size, average file size, and information on the amount of filesystem storage used.
Here is an example of output from db.stats() using the sweetscomplete database:
Information given by db.<COLLECTION>.stats() (substitute the name of the collection in place of <COLLECTION>), a wrapper for the collStats database command, easily produces 10 times the amount of information as db.stats(). The output from db.<COLLECTION>.stats() gives the following general information:
If all you need to do is to find information on the amount of storage space used by MongoDB collection components, a number of shortcut methods have been created that leverage db.collection.stats(). The following fall into this category:
For more information on db.collection.stats(), see https://docs.mongodb.com/manual/reference/method/db.collection.stats/index.html#db-collection-stats.
For more information on capped collections, see https://docs.mongodb.com/manual/core/capped-collections/index.html#capped-collections.
For more information on WiredTiger, see https://docs.mongodb.com/manual/core/wiredtiger/index.html.
Let's now turn our attention to server status.
Monitoring server status
Another extremely useful shell method used to gather information about the state of the database server is db.serverStatus(). This shell method is a wrapper for the serverStatus. database command. As with the stats() method described in the previous sub-section, this utility provides literally hundreds of statistics. An explanation of all of them is simply beyond what this book is able to cover.
Here is a screenshot of the first few statistics:
Here is a brief summary of the more important statistics:
This table provides statistics that might indicate problems with your database:
And that concludes our coverage of essential MongoDB administration techniques.