Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'explain' a runCommand?

Tags:

mongodb

Is there a way to run explain with runCommand? I have the following query:

db.runCommand({geoNear:"Locations", near:[50,50], spherical:true})

How can I run an explain on it? I want to get the execution time.

like image 655
chaliasos Avatar asked Sep 09 '26 04:09

chaliasos


2 Answers

As far as I know, explain is a method on cursors. You, however, can enable the integrated mongodb profiler:

db.setProfilingLevel(2); // log all operations
db.setProfilingLevel(1, 50); // log all operations longer than 50msecs

This will log details like nscanned, nreturned to a capped collection called system.profile, but does not provide as much detail as an explain() call does.

In this case, however, I think it might be possible to change the runCommand to a $near-query instead? That would give you full access to explain.

like image 109
mnemosyn Avatar answered Sep 10 '26 17:09

mnemosyn


I guess we can't do explain for runCommand. Some of the runCommand give you the stats automatically ( eg. distinct command : db.runCommand({distinct : 'test', key : 'a'}) )

But, you can take help of the query profiler.

db.setProfilingLevel(2)

Once you run that query, switch off the profiler, and check the system.profile collection for this query.

like image 37
Abhishek Kumar Avatar answered Sep 10 '26 19:09

Abhishek Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!