Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query times in mongoose #6376

Closed
suchitagarwal opened this issue Apr 20, 2018 · 6 comments
Closed

Query times in mongoose #6376

suchitagarwal opened this issue Apr 20, 2018 · 6 comments

Comments

@suchitagarwal
Copy link

suchitagarwal commented Apr 20, 2018

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

@suchitagarwal suchitagarwal changed the title Query sizes in mongoose Query times in mongoose Apr 22, 2018
@vkarpov15
Copy link
Collaborator

Use query middleware

@suchitagarwal
Copy link
Author

Hi @vkarpov15, thanks for the reply. Just wondering if there are any examples of using query middleware, as I am not exactly clear on how this would work.

@suchitagarwal
Copy link
Author

Also, I would have to set a timer in the pre middleware and have it available in the post, not sure if just setting it on this would actually persist

@vkarpov15
Copy link
Collaborator

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);
  }
});

@suchitagarwal
Copy link
Author

Thanks @vkarpov15, I should have replied earlier. I tried that and it worked. Thanks for your help

@vkarpov15
Copy link
Collaborator

Happy to help 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants