WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MongoDB · Advanced · question 53 of 100

How do you monitor and manage slow queries in MongoDB?

📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

Monitoring and managing slow queries in MongoDB is crucial for ensuring good performance and optimizing your database. Here are several ways to do it:

1) Use MongoDB’s built-in profiler:
MongoDB has a built-in profiler that logs information on slow operations. The profiler can be configured to log information on slow queries, commands, and database operations. The profiler data can be used to identify slow queries and optimize them. There are three profiling modes:

- Off (0): profiling is disabled.

- Slow Op (1): profiles operations slower than specified query execution time in milliseconds (by default 100ms).

- All Op (2): profiles all operations.

To enable profiling, set the profiling level to either 1 or 2 for a specific database using the following command:

db.setProfilingLevel(1, { slowms: 100 })

This command sets the profiling level to Slow Op and logs queries that take more than 100 milliseconds to execute.

To view the profiling data, use the following command:

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

2) Use MongoDB Compass:
MongoDB Compass is a graphical user interface for MongoDB that can be used to monitor and manage slow queries. Compass has a built-in performance analysis tool that provides visibility into query and index usage metrics. It can help you identify slow queries and optimize them easily by providing an easy-to-use UI.

To access this tool, click on the ‘Performance’ tab and select the desired database and collection. This will bring up a dashboard that shows useful metrics such as slow queries, index usage, and query patterns.

3) Use third-party tools:
Several third-party monitoring and management tools are available for MongoDB that can help with monitoring slow queries. Some popular options include:

- MMS (MongoDB Management Service): a cloud-based monitoring tool provided by MongoDB Inc.

- Ops Manager: an on-premise monitoring and management tool provided by MongoDB Inc.

- Datadog: a third-party monitoring tool that provides integration with MongoDB.

In conclusion, monitoring and managing slow queries in MongoDB is important to maintain database performance. Using the built-in profiler, MongoDB Compass or third-party tools, you can easily identify slow queries and optimize them for better performance.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview — then scores it.
📞 Practice MongoDB — free 15 min
📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

All 100 MongoDB questions · All topics