Measure time of query in Mongo

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

Upvotes: 38

Views: 57528

Answers (3)

Himanshu Gupta
Himanshu Gupta

Reputation: 79

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

Upvotes: 3

Rohit Patwa
Rohit Patwa

Reputation: 1160

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

Upvotes: 86

James Crosswell
James Crosswell

Reputation: 786

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.

Upvotes: 16

Related Questions