Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the maximum execution timeout of aggregation

I am developing an application where I use MongoDB with Mongoose. There are some query which takes long time to give result.I know that $maxTimeMS can set maximum time for any find query but I want to set or increase query execution time for any aggregate method.How can I do that?

like image 941
Ronit Sarma Avatar asked Oct 29 '25 10:10

Ronit Sarma


1 Answers

MongoDB's own node driver allows you to make the aggregate method use a cursor (it does not do this by default). When using a cursor, you can also provide it a maxTimeMS option to increase/decrease the timeout on the aggregate operation. Documented here

To do the same in Mongoose, you need to access the raw collection object for your model:

YourModel.collection.aggregate([/* pipeline */], {
  cursor: {
    batchSize: /* an appropriate batch size */
  },
  maxTimeMS: 60000
});

However, operations that run this long on MongoDB are not a good idea, so you should look into optimizing your pipeline or breaking it down. You may also want to make sure your collection is properly indexed.

like image 60
Aakash Jain Avatar answered Nov 01 '25 13:11

Aakash Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!