How to Configure MongoDB Database Profiling using Java

Introduction

If you’re using MongoDB to store and manage your data, it’s important to keep the database running as efficiently as possible. The MongoDB database profiler can help you accomplish this by identifying slow operations and queries. With this information in hand, you can make changes to boost your database’s performance, such as rewriting slow queries or optimizing a database’s schema. You can configure the database profiler to run at different levels, depending on your specific needs. In this article, we’ll explain how to configure MongoDB database profiling using Java.

Prerequisites

Before we begin looking at the Java code, let’s review the system requirements needed to proceed. For this task, there are only a couple of prerequisites:

  • You’ll need to confirm that MongoDB and the MongoDB Java driver are installed and configured beforehand.

  • In this article, we assume that the MongoDB version used is V4.0.9.

What is the MongoDB database profiler?

In MongoDB, the database profiler is a tool that collects information about the database operations that are run against a MongoDB instance. The collected data can help identify inefficient queries and operations; you can use this information to fine-tune your queries and optimize schemas.

The MongoDB Profiling Level

The MongoDB database profiler has three different levels of operation:

  1. Level 0 — The profiler turned off at this level and will not log any data.
  2. Level 1 — The profiler will only log slower operations where the execution time exceeds a specified threshold.
  3. Level 2 — At this level, the profiler will log all operations regardless of execution time.

MongoDB Database Profiling using Mongo Shell

How to Profile Check MongoDB using Mongo Shell

At this point, we’re ready to configure the profiler settings for a database. Let’s begin by retrieving a list of all databases on the server. To do this, we can use commands available in the Mongo shell. Run the following command in the terminal window:

show dbs

The result will look something like this:

playerDB 0.000GB
db 0.000GB
local 0.000GB

Next, we’ll select a database to configure:

use playerDB

The result will look like this:

switched to db playerDB

Finally, we’ll get the current profiling status:

db.getProfilingStatus()

This result will resemble the following:

{ "was" : 0, "slowms" : 100, "sampleRate" : 1 }

Notice the value "was" : 0— this indicates the current profile setting. The value of 0 means that the profiler is off and will not log any data. The second value "slowms" : 100 refers to the threshold used for level 1 profiling and is measured in milliseconds. In this case, when the profiler is set to level 1, it will only log queries that take more than 100 milliseconds to execute.

Let’s change the profile level, using the command shown below:

db.setProfilingLevel(2)

This will set the profile to 2, which enables MongoDB to log all operations.

How to Configure MongoDB Database Profiling in Eclipse IDE using Java

In the previous section, we showed how to configure the profiling level for a database via the Mongo shell. Next, we’ll perform the same configuration using Eclipse IDE and Java.

The code shown below will set the profile to “2”.

MongoClient mongo = MongoClients.create("mongodb://127.0.0.1:27017"); // (1)
MongoDatabase db = mongo.getDatabase("PlayerDB"); // (2)
db.runCommand((Bson) BasicDBObjectBuilder.start().add("profile", 2).get() ); // (3)

Let’s look at what just happened in this code:

  1. First, we instantiate the MongoClient and connect to the MongoDB deployment.
  2. Then, we get the MongoDB database "PlayerDB" and set it to the variable db.
  3. Finally, we use runCommand, which can be used to process any given command in relation to the current database. BasicDBObjectBuilder is used to create objects. Combining these two allows you to successfully change the MongoDB profile level.

If you’d like to verify that the Java code executed correctly, run the command shown below in the Mongo shell:

db.getProfilingStatus()

You should see results that look like the following:


GeSHi Error: GeSHi could not find the language json (using path /nas/content/live/orkbprod/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

You can see that the current profiling level is 2, proving that our configuration change was successful. Alternatively, you can also confirm that the setting change was successful using the following command:

db.getProfilingLevel()

In this case, the result will be:

2

Again, we can see that the current level is 2, which confirms our configuration change was successful.

Conclusion

When you’re working with MongoDB, it’s important to keep an eye on performance. Fortunately, the MongoDB database profiler helps you accomplish that task by logging database operations so that you can identify slow queries. You can configure the profiler’s settings to achieve the level of logging that makes sense for your organization’s needs. With the instructions provided in this tutorial, you’ll be ready to configure MongoDB database profiling using Java or the Mongo shell.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.