MongoDB Database Profiler for Monitoring Queries and Performance

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

Learn all about MongoDB Database Profiler for Monitoring Database Queries and Performance:

In this Free MongoDB training series, we learned about MongoDB Performance in our previous tutorial.

In this tutorial, we will learn all about MongoDB Database Profiler in detail.

Database profiler is used to collect information regarding the queries which are executed on an individual database instance.

11. Manage Mongodb database profiler

If you are working with an enterprise level application and if you have been simultaneously executing queries then maybe in some queries you have to face a deadlock.

In order to identify the query in which you are facing the deadlock or any kind of issues, there is a feature called profiler. MongoDB is also providing this feature to record the log of an individual query which is executed. These logs record all the crud operations along with configuration and management controls.

By default, all data is recorded within the system.profiles collection within the MongoDB admin instance.

The profiler is disabled due to the high consumption of memory by default. There are three different levels of the profiler to record the information regarding the queries and you can easily set any level of profiler on any instance of MongoDB.

Enable and Configure Profiling for Databases

Database profiler is activated by the profile command with the help of the mongo shell. Whenever you are activating profiler to log the record of query execution, then you have to mention the level of profiling. With the help of the following code, we are going to enable profiling for MongoDB.

Syntax

db.setProfilingLevel(LEVEL)

Code

db.setProfilingLevel(2)

Figure 1: In Mongo Shell

Enable and configure profiling for databases in mongodb shell

Figure 2: In Robo 3T

Enable and configure profiling for databases in robo 3t

In the above image, you can observe that there are four outcomes. In the first field, it is showing the previously used profile level and the last field is indicating the success of the operation.

Check the Level of Profiling

To preview the current level of the profiler, you have to use the following code.

Code

db.getProfilingStatus()

It will show you the current and previously used profiler status.

Figure 3: In Mongo Shell

Check the level of profiling in mongodb

Figure 4: In Robo 3T

Check the level of profiling in Robo 3t

  • was the current level of profiling.
  • slowms field shows the operating time limit in milliseconds.
  • SampleRate shows the percentage of slow operations to be profiled.

To get only the profiler level, you can use the db.getProfilingLevel () in the mongo shell.

Code

db.getProfilingLevel()

Figure 5: In Mongo Shell

ProfilingLevel() in mongodb

Figure 6: In Robo 3T

ProfilingLevel() in Robo 3t

Deactivate Profiling

If you want to deactivate the profiler, you can use the following code to stop logging the query execution information.

Code

db.setProfilingLevel(0)

Figure 7: In Mongo Shell

Deactivate Profiling in mongodb

Figure 8: In Robo 3T

Deactivate Profiling in Robo 3t

Overhead Profiler

When you are logging the record of query execution or you are using the profiler, then it would likely affect the performance of query execution. By default, the profiler collection has 1 MB as a memory to store the information.

If you have a huge application and a lot of transactional data, then it will be overhead to store a lot of information as a profiler.

Change the Size of the system.profile Primary Collection

Before you are going to change system.profiles collection size, you must do the following things:

  1. Deactivate profiling
  2. Drop the collection system.profile
  3. Create a new.profile system collection
  4. Reactivate profiling

Code

db.setProfilingLevel(0)
db.system.profile.drop()
db.createCollection( "system.profile", { capped: true, size:4000000 } )
db.setProfilingLevel(1)

Figure 9: In Mongo Shell

Modify the size of the system. profile primary collection in mongodb

Figure 10: In Robo 3T

Change the size of the system. profile primary collection robo3t

Conclusion

MongoDB database profiler is used to monitor the queries and their performance within the MongoDB instance. We can monitor queries on different levels of profiling as we discussed in the previous tutorial.

In this tutorial, we have successfully learned how to switch the level of profiling and how we can disable them as per our requirement. We can also set up the required threshold to store the profiler record.

Our upcoming tutorial will explain to you about User creation and assigning roles in MongoDB!!

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment