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

Add maxTime option to all queries #4066

Closed
tomgrossman opened this issue Apr 12, 2016 · 22 comments
Closed

Add maxTime option to all queries #4066

tomgrossman opened this issue Apr 12, 2016 · 22 comments
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@tomgrossman
Copy link

I would like to add the option for maxTime(ms) for all models queries (find, update, findOneAndUpdate, etc..).

for example:

A.findOneAndUpdate(id, updateObject, {maxTime: 1000}}, callback)

@vkarpov15
Copy link
Collaborator

Write a plugin, use query middleware

@tomgrossman
Copy link
Author

Hi @vkarpov15 ,
After further investigation about this issue, it seems that this option exists, but not in the API.
I added maxTime: x to the options object in findOneAndUpdate function, and I saw that it is being converted to maxTimeMS option in the mquery.js file in this function:
Query.prototype.maxTime = function (v) (line 1359).

So it looks like this option is working, am I wrong?
it doesn't appear in the API in the available options.

@vkarpov15
Copy link
Collaborator

@tomgrossman yep should be working, just added it to the docs.

@tomgrossman
Copy link
Author

tomgrossman commented Apr 18, 2016

@vkarpov15 So there is no need for a plugin, right? Should be closed.

@vkarpov15
Copy link
Collaborator

I think the original issue was "ability to set maxTimeMS for all queries" which is a task for a plugin. Adding the option to the docs is separate.

@tomgrossman
Copy link
Author

@vkarpov15 sorry I don't understand. I said before that this option already exists but was only missing in the docs. So there is no need for a plugin. Am I wrong?

@vkarpov15
Copy link
Collaborator

The plugin is for setting max time ms for all queries at once, like mongoose.use(maxTimeMSPlugin(5000)) will make it so that all queries have maxTimeMS of 5000 by default. I thought that was the original use case, no?

@tomgrossman
Copy link
Author

@vkarpov15 no, the original case was the option to add this timeout for every query. as an option.
but this is already available.

@vkarpov15
Copy link
Collaborator

Ok then we can close this issue :)

@santimendoza
Copy link

Hey just to know, find, findOne does not have the maxTime option? I can't see it in the documentation, neither seems to work.

@vkarpov15
Copy link
Collaborator

@santimendoza the option is maxTimeMS http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#findOne we will add it to the mongoose docs

@vkarpov15 vkarpov15 reopened this Jan 16, 2018
@vkarpov15 vkarpov15 added this to the 4.13.10 milestone Jan 16, 2018
@freewil
Copy link
Contributor

freewil commented Mar 13, 2019

The plugin is for setting max time ms for all queries at once, like mongoose.use(maxTimeMSPlugin(5000)) will make it so that all queries have maxTimeMS of 5000 by default. I thought that was the original use case, no?

@vkarpov15 I was just looking to do this - would the plugin approach still be the best way to achieve global query timeouts in latest mongoose v5?

@freewil
Copy link
Contributor

freewil commented Apr 8, 2019

bump

@vkarpov15 I was just looking to do this - would the plugin approach still be the best way to achieve global query timeouts in latest mongoose v5?

@vkarpov15
Copy link
Collaborator

@freewil yes it would. I'm going to be writing a blog post on maxTimeMS next week that you should keep an eye out for. MaxTimeMS has some caveats

@vkarpov15 vkarpov15 reopened this Apr 13, 2019
@vkarpov15 vkarpov15 modified the milestones: 4.13.10, 5.6 Apr 13, 2019
@vkarpov15 vkarpov15 added the new feature This change adds new functionality, like a new method or class label Apr 13, 2019
@natanavra
Copy link

@vkarpov15 Hi!
Seems docs are not updated, as Query.prototype.maxTimeMS errors with is not a function
Query.prototype.maxTime works as expected.

@vkarpov15
Copy link
Collaborator

@natanavra can you write a script that demonstrates this issue and clarify which version of Mongoose you're using? Here's the maxTimeMS() function in our code

@natanavra
Copy link

natanavra commented May 20, 2019

@vkarpov15 using Mongoose 5.0.16

Here's an example:

return await model
  .find(query, null)
  .sort(sort)
  .maxTime(constants.maxQueryMS) //maxTimeMS here does not work
  .hint(model.getHint())
  .limit(constants.maxEvents)
  .cache(10)
  .catch(err => {
     logger.warn({err}, 'events.find failed');
  });

@Fonger
Copy link
Contributor

Fonger commented May 21, 2019

@natanavra You can't use that in mongoose 5.0.16. It was added in mongoose 5.2.6

I added maxTimeMS alias to maxTime in mquery v3.1.1, an underlying library of mongoose. This was introduced since [email protected], which used this version of mquery.

mongoosejs/mquery#105
mongoosejs/mquery@42499d3

For those who uses TypeScript, my PR for Query#maxTimeMS is awaiting to be merged DefinitelyTyped/DefinitelyTyped#35677 and will be released as @types/[email protected]

@natanavra
Copy link

@Fonger Ahh thanks for letting me know
There is not mention of it in the docs https://mongoosejs.com/docs/api.html#query_Query-maxTimeMS
And I never really accessed previous 5.x.x docs since it's usually the latest.

@GeniusBrother
Copy link

I can't find maxTimeMSPlugin, so is this just an example? @vkarpov15

@vkarpov15
Copy link
Collaborator

@GeniusBrother we added a global and connection level maxTimeMS option in 5.6.0.

mongoose.set('maxTimeMS', 3000);

Or:

const db = mongoose.createConnection(uri);

db.set('maxTimeMS', 3000);

@Automattic Automattic locked as resolved and limited conversation to collaborators Jun 16, 2019
@vkarpov15
Copy link
Collaborator

See Connection#set() and Mongoose#set()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

8 participants