34

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()

3 Answers3

80

You can add .explain("executionStats") at the end of the query.

Rohit Patwa
  • 1,092
  • 1
  • 9
  • 12
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")