Video Transcript (English)
So you want to unlock the secrets of your database's performance at a more granular level? Let’s take a look at some of MongoDB's built-in tools to do this.
In this video, we'll explore two MongoDB shell commands: db.serverStatus(), db.stats().
These commands provide detailed metrics about any MongoDB deployment. While it’s beyond the scope of this video to discuss all the possible metrics to look at, it’s worth noting that the commands allow you to extract specific metrics and build custom monitoring solutions.
Let's start by taking a look at the overall health of our MongoDB server with the db.serverStatus() command.
The db.serverStatus() command provides a wrapper around the serverStatus database command and gives us a real-time view of our server's health.
It is very important to understand that the server status command output represents all databases and activity, so we can't use it to drill down into the performance of a single database or collection.
To use db.serverStatus, we need to be running the MongoDB shell. Once logged into the MongoDB shell, we can execute the command as seen here.
Whoa! That is a lot of useful information but it’s more than we need right now. For more information on all of the metric categories, be sure to check out the MongoDB documentation.
Fortunately, we can limit the output to the areas that we want. For now, let’s just look at information about operation counts and locks. We’ll limit the output by appending the category name to the serverstatus command.
Let's start with the operation counts.
To get the operation counts information, we execute the serverStatus command with opcounters appended to the end, like this.
This tracks the total number of database operations like queries, inserts, updates, and deletes since the last server restart. High opcounters for specific operations help us identify which query types to focus our optimization efforts on.
To understand the rate of operations and identify potential bottlenecks, we need to compare the output of db.serverStatus() taken at different points in time. For instance, running the command a few times during a period of time and comparing the differences in the counters could give you a sense of the increased operational load.
Now, let’s look at the locks category. It provides information about concurrency, which is your database’s ability to handle multiple operations at the same time.
We pull the information by appending locks to the end of the serverStatus command.
High values in the timeAcquiringMicros field can indicate that multiple operations are competing to acquire the same resource, slowing down performance.
Finally, let's zoom out a bit and look at the overall health of our databases.
The db.stats() command summarizes key metrics related to storage, document count, and indexing. It’s a useful tool for seeing how efficiently your database and indexes are using available disk space.
To use db.stats, we need to be running the MongoDB shell. The command returns a JSON document containing statistics about the currently selected database. This gives us high-level statistics about our database's resource consumption, including the number of collections, the total storage size, the number of documents, and details about our indexes.
There is a lot of information here so let’s talk about some fields that may be useful for you to monitor.
First, we want to pay close attention to the dataSize value. This tells us how much disk space our database is currently occupying.
An unexpected increase in data size during testing may highlight issues such as redundant or outdated data occupying disk space which could lead to increased latency or decreased throughput.
Next, the objects field shows the total number of documents across all collections in the database, the indexes field tells us how many indexes we have, and the indexSize indicates the total size of all indexes.
Large indexSize relative to dataSize suggests over-indexing, meaning we should review and potentially consolidate indexes.
Check out our skill on indexing design for more on that topic.
Lastly, high dataSize with low objects count might indicate large documents that could benefit from schema redesign or compression.
Congratulations! In this video, we learned how to use the db.serverStatus() command to get a real-time snapshot of the MongoDB server, focusing on operation counts, and locks to understand server load and concurrency for all databases.
Then, we used the db.stats() command to get a high-level overview of the database, including storage size, document count, and index information to assess overall resource consumption.
