How to find the slow queries on MongoDb ๐Ÿข

If you are experiencing performance issues on your MongoDb, one of the things to check is what is consuming most of the time. Let's see how to find the slow queries on MongoDb

Enable profiling ๐Ÿ“Š

First of all, we must to active profiling in order to collect data of the operations. So connect to your MongoDb and execute:

// Collect about all operations

If you want to collect only for the queries above 100ms, just run:

// Collect about operations that take longer than +100ms
db.setProfilingLevel(1, { slowms: 100 });

In order to check if there was any previous level or confirm the new one, run:


Example output:

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

Example of first-time activation ("was":0) to collect longer than 100ms ("slowms":100) and profiling is fine ("ok": 1)

When you wish to disable it, run:

// Disable profiling

๐Ÿ’ก More info in profiling doc

Find the slow queries ๐Ÿข

The operations data is stored on db.system.profile collection.

Then in order to see the top 10 slow queries, run:

db.system.profile.find().limit(10).sort( { millis : -1 } ).pretty()

Slowest first. Depending on operation type (insert, update, aggregation, etc.) you will find different fields and data.

So you can check if it's using indexes, if it's scanning more rows than needed and ultimately, what operations you need to take care first ๐ŸŽฏ

๐Ÿ’ก If you are dealing with performance issues, let me recommend you the performance guide on MongoDb doc

Leave a comment below with any doubt or question ๐Ÿ‘‡

David Burgos

Read more posts by this author.