Skip to main content



MongoDB - Register function to get execution time of a query

 

Register a script function like below - 

function time(command) { const t1 = new Date(); const result = command(); const t2 = new Date(); print("Execution time: " + (t2 - t1) + "ms"); return result; }


Then run query like below - 

time(()=>  db.test.aggregate( { $match: { macAddress: { $regex: "^XX:?08:?XX:?1X:?2X:?X5$(?i)", $options: "i" }, requestTimestamp: { $gte: ISODate("2025-11-04T00:00:00Z") } } }, { $sort: { requestTimestamp: -1 } }, { $limit: 100 } ).toArray());


That should print execution time and results - 

Execution time: 152ms

[{....}, {...}]

Comments