Development

How to Log All Queries in MongoDB

Captain Salem 2 min read

How to Log All Queries in MongoDB

Logging is probably one of the default methods system administrators and developers dive into when it comes to debugging and performance optimization. Whether it’s memory leaks, errors, or just security exploration, logs are incredible tools.

In this tutorial, we will explore how we can enable logging for all queries in MongoDB. This will allow you to keep track and trace any queries executed on the server.

Method 1 - Using MongoDB Profiling

MongoDB includes a database profiling feature that allows us to records CRUD (Create, Read, Update, Delete) operations as well as configuration and administration commands.

By default, MongoDB profiling is disabled. However, we can enable it is few simple steps.

Enable Profiling Using MongoDB Shell

Start by connecting to the MongoDB instance using the mongo shell.

mongo

Next, switch to the database on which you wish to enable profiling:

use <database_name>

Finally, enable profiling using the db.setProfilingLevel() function.

For example, to enable profiling for all operations:

db.setProfilingLevel(2)

The command should enable profiling for all operations on the selected database and return an output as shown:

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

Supported MongoDB Profiling Levels

MongoDB supports the following profiling levels:

  1. Level 0 - Profiling Off - This level turns off profiling.
  2. Level 1 - Profile Slow Operations Only - Only operations that take longer than the value of slow operation threshold (slowms) are profiled. This level is useful when you want to find out which operations are slowing down the database, or if you only want to log a subset of operations to avoid large overheads in busy systems.
  3. Level 2 - Profile All Operations - All operations are profiled, regardless of their execution time. This is the most detailed level and includes information about every database operation. This level can create a significant amount of profiling data, especially in a busy system, and thus might impact the performance. Therefore, it’s recommended to use this level judiciously and for short period.

Enable Profiling in Configuration File

We can also enable profiling by configuring the operationProfiling.mode setting in the MongoDB configuration file.

Locate and edit the mongod.conf or mongos.conf configuration file in your system.

Next, locate the operationProfiling.mode parameter and set the value to all as shown in the example below:

operationProfiling:
    mode: all

Save the changes and restart the MongoDB service.

Method 2 - Using Mongod/Mongos Log Messages

If you only want to log operations for a short period of time, you can use the mongod or mongos setParameter command with the logLevel parameter.

Start by connecting to the mongod or mongos instance with the mongo shell.

mongo --port 27017

Next, run the setParameter command.

db.adminCommand( { setParameter: 1, logLevel: 2 } )

This should update the log level to log all operations for that session.

NOTE: Ensure to revert the logLevel back to its original value to prevent unnecessary performance degradation or excessive disk space consumption.

db.adminCommand( { setParameter: 1, logLevel: 0 } )

Viewing The Logs

Once you’ve enabled logging, you can access the logs from the MongoDB logs file or from the system.profile collection in the MongoDB shell.

Accessing logs from the logs file

The default location of the MongoDB logs file depends on your operating system. For example, on Ubuntu, the default log file is at /var/log/mongodb/mongod.log. You can use commands such as less, cat, etc to read the logs. You can also export the logs to a log processing system such as Grafana, Elasticsearch, etc.

Accessing logs from the system.profile collection

Once profiling is enabled, MongoDB will write the profiling data to the system.profile collection in the current database. You can query this collection like any other in the database:

db.system.profile.find().pretty()

You now have a complete guide on logging all queries in MongoDB. Remember, excessive logging can affect database performance, so use these tools judiciously.

Conclusion

In this tutorial, we learned how we can enable all query logging in MongoDB by using the various tools provided in the Mongo Server. We hope you enjoyed this tutorial!!

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.