How can I measure execution time of query in MongoDB ? I found Mongo-hacker plugin but it looks like it measure time of query including time of displaying all results. In PostgreSQL I use Explain Analyze SELECT ... , but i didn't found any information about time in mongo's db.collection.find({smth}).explain()
Asked
Active
Viewed 4.8k times
3 Answers
80
You can add .explain("executionStats")
at the end of the query.

Rohit Patwa
- 1,092
- 1
- 9
- 12
-
8If you need execution stats for an `aggregate` query, look here: https://jira.mongodb.org/browse/SERVER-19758 – Manuel Jun 04 '18 at 10:23
-
1What about measuring execution time of insert operation? – Paweł Raglis Jan 19 '23 at 21:10
-
How to find execution time for updateOne operation ? – Jet Mar 20 '23 at 09:56
17
The easiest way is to set the profiling level in MongoDB: https://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/
Once you've done this, details on all of your queries will be logged to the system profile table.

James Crosswell
- 648
- 4
- 13
2
Using Robo 3T and this worked for me in aggregation -
db.getCollection('product_verification').explain("executionStats").aggregate([
{$match: {"company_id":1}},
{$lookup: {
"from": "product",
"let": {"company": "$company_id", "code": "$item_code"},
"pipeline": [
{$match: {$expr: {$and: [
{"$eq": ["$company_id", "$$company"]},
{"$eq": ["$item_code", "$$code"]},
{"$in": ["$brand_uid",[13, 20]]}
]}
}}
],
"as": "details"
}}])
And when using not aggregation
db.getCollection('product').find({name: "lenovo"}).explain("executionStats")

Himanshu Gupta
- 69
- 3