How to log all or slow MongoDB queries?

Better Stack Team
Updated on August 25, 2023

To log all or slow MongoDB queries, you can use MongoDB's built-in logging capabilities and monitoring tools. MongoDB provides various options for capturing query performance and activity. Here's how you can enable query logging and monitor slow queries:

1. Enable Query Logging

MongoDB allows you to log queries to a file or the system log. To enable query logging, follow these steps:

  • Edit your MongoDB configuration file (mongod.conf), which is typically located in the /etc directory on Linux systems.
  • Add or modify the following settings in the configuration file:

    # Enable logging to a file
      destination: file
      path: /var/log/mongodb/mongod.log
      logAppend: true
    # Enable query logging (verbose mode)
      mode: slowOp
      slowOpThresholdMs: 100 # Adjust the threshold (in milliseconds) for slow queries
  • Make sure to create the specified log directory (e.g., /var/log/mongodb/) and ensure that the MongoDB process has the necessary permissions to write to the log file.

  • Restart the MongoDB server for the changes to take effect.

2. Monitor Slow Queries

MongoDB's built-in profiling can be used to monitor slow queries. The operationProfiling setting in the configuration file, as shown above, enables this feature. You can also configure the threshold for slow queries by adjusting the slowOpThresholdMs value.

To view the slow queries that MongoDB has captured, you can use the db.currentOp() command in the MongoDB shell:

db.currentOp({ "op" : "query", "millis" : { "$gt" : <threshold_in_milliseconds> } })

Replace <threshold_in_milliseconds> with the desired threshold. This command will display currently executing queries that have exceeded the specified threshold.

3. Third-party Monitoring Tools

MongoDB offers a variety of third-party monitoring and management tools that provide more advanced query analysis and performance insights. Some popular tools include:

  • MongoDB Atlas: If you're using MongoDB Atlas (MongoDB's cloud-hosted service), it includes built-in monitoring and performance analysis features.
  • MMS (MongoDB Management Service): MongoDB's legacy monitoring service provides performance and query analysis capabilities.
  • Ops Manager: MongoDB's newer management platform includes monitoring, automation, and performance analysis tools.

Remember to periodically review your logs and query performance to identify optimization opportunities and potential issues.

To learn more about logging, visit Better Stack Community.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →