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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github