You can try to set connectionTimeoutMS and socketTimeoutMS to ‘0’, hoping it to never time out, but ‘0’ doesn’t mean never with Mongo (from the FAQ)
The special meaning of 0
Setting connectTimeoutMS and socketTimeoutMS to the value 0 has a special meaning. On the face of it, it means never timeout. However this is a truth with some modifications. Setting it to 0 actually means apply the operating system default socket timeout value.
maxTimeMS is the option you are looking for
Most people try to set a low socketTimeoutMS value to abort server operations. As we have proved above this does not work. To work correctly you want to use the maxTimeMS setting on server operations. This will make MongoDB itself abort the operation if it runs for more than maxTimeMS milliseconds. A simple example is below performing a find operation.
For long running applications, it is often prudent to enable keepAlive with a number of milliseconds. Without it, after some period of time you may start to see “connection closed” errors for what seems like no reason. If so, after reading this, you may decide to enable keepAlive: