Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb: how can I see the execution time for the aggregate command?

Tags:

mongodb

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 query execution time? Is there any equivalent function for explain method for aggregation queries.

like image 663
alessio1985 Avatar asked Dec 24 '12 12:12

alessio1985


People also ask

How fast is MongoDB aggregation?

600MB/s maximum instantaneous data transfers. 7200 RPM spindle. Average latency = 4.16ms.

Which is faster in find and aggregate in MongoDB?

find. The aggregation query takes ~80ms while the find query takes 0 or 1ms.

What are the differences between using aggregate () and find () in MongoDB?

With aggregate + $match, you get a big monolithic BSON containing all matching documents. With find, you get a cursor to all matching documents. Then you can get each document one by one.


2 Answers

var before = new Date() #aggregation query var after = new Date() execution_mills = after - before 
like image 178
Dmitry Zagorulkin Avatar answered Sep 21 '22 06:09

Dmitry Zagorulkin


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

like image 24
Lukasz Wiktor Avatar answered Sep 19 '22 06:09

Lukasz Wiktor