question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query times in mongoose

See original GitHub issue

Do you want to request a feature or report a bug? feature

What is the current behavior? N/A

If the current behavior is a bug, please provide the steps to reproduce. N/A

What is the expected behavior? A way to log the time taken by each mongoose query

Context: We recently had an issue where our mongo instances were thrashing because we were running a really large and slow query. The size of the query itself was about 5MB and it was taking minutes to execute. I would like to look at each query that we send to mongo and log something if the time taken by the query is above a threshold. The mongoose.('debug', () => {}); only allows to log the query before it’s sent, but no way to find the time taken by the query

Please mention your node.js, mongoose and MongoDB version. node: v8.9.1 mongoose: 4.11.11 mongo: 3.4

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

19reactions
vkarpov15commented, Apr 27, 2018
schema.pre('find', function() {
  this._startTime = Date.now();
});

schema.post('find', function() {
  if (this._startTime != null) {
    console.log('Runtime in MS: ', Date.now() - this._startTime);
  }
});
0reactions
vkarpov15commented, Apr 27, 2018

Happy to help 🍻

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to trace Mongoose query execution time - Stack Overflow
I'm trying to create trace spans of my node. js with Mongoose application. One specific trace I would like to get is how...
Read more >
mongoose-execution-time - npm
Mongoose plugin for measuring and logging query execution time. Latest version: 1.0.2, last published: 2 years ago.
Read more >
Mongoose v6.8.2: Queries
A mongoose query can be executed in one of two ways. First, if you pass in a callback function, Mongoose will execute the...
Read more >
Simple query taking long time to execute - MongoDB
I have my collection with less than 10.000 documents and Im running the following query: Sale.aggregate([ { $match: { seller, createdAt: { $gte: ......
Read more >
How to optimize MongoDB & Mongoose for Performance
The more requests you do the slower your app response time becomes. Try to minimise your database queries as much as possible and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found