I execute the follow mongodb command in mongo shell
db.coll.aggregate(...)
and i see the list of result. but is it possible to see the quer
var before = new Date()
#aggregation query
var after = new Date()
execution_mills = after - before
Try .explain("executionStats")
:
db.inventory.find(
{ quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")
Which returns a very detailed JSON with the time in "executionTimeMillis"
like:
{
"queryPlanner" : {
...
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 3,
"executionTimeMillis" : 0,
...
More details: https://docs.mongodb.com/manual/tutorial/analyze-query-plan/#evaluate-the-performance-of-a-query
Or you can install the excellent mongo-hacker, which automatically times every query, pretty()
fies it, colorizes the output, sorts the keys, and more:
I will write an answer to explain this better.
Basically there is no explain() functionality for the aggregation framework yet: https://jira.mongodb.org/browse/SERVER-4504
However there is a way to measure client side but not without its downsides:
However that being said, you might be able to get a slightly accurate result by doing it in MongoDB console on the same server as the mongos
/ mongod
. This will create very little in betweens, still too many but enough to maybe get a reading you could roughly trust. As such you could use @Zagorulkin's answer in that position.
You can add a time
function to your .mongorc.js file (in your home directory):
function time(command) {
const t1 = new Date();
const result = command();
const t2 = new Date();
print("time: " + (t2 - t1) + "ms");
return result;
}
and then you can use it like so:
time(() => db.coll.aggregate(...))
Caution
This method doesn't give relevant results for db.collection.find()
i see that in mongodb there is a possibility to use this two command:
db.setProfilingLevel(2)
and so after the query you can use db.system.profile.find() to see the query execution time and other