Search code examples
node.jsmongodbmongoose

Connection Timeout for mongodb using mongoose


I have a web application running on Node, express and MongoDB. I use mongoose as the ODM. When i tested my application with mongodb version v3.0.1 it runs fine and throws no errors. But when i run the same code v3.2.10 i get a connection timeout after some time.

I get the following Error :

Error: connection timeout at null.<anonymous> (/webapp/node_module/mongoose/lib/drivers/node-mongodb-native/connection.js:186:17)

I use mongoose.connect for the db connection to the local mongodb instance. Has anything changed in the way of connection ?


Solution

  • I had this problem a while ago. It all depends on which version of mongoose and mongodb-core you are using. Right now, you have to specify the following parameters:

    mongoose.connect("mongodb://user:password@address/db", {
      server: {
        socketOptions: {
          socketTimeoutMS: 0,
          connectionTimeout: 0
        }
      }
    });
    

    However, just yesterday, the correct parameters where

    mongoose.connect("mongodb://user:password@address/db", {
      server: {
        socketOptions: {
          socketTimeoutMS: 0,
          connectTimeoutMS: 0
        }
      }
    });
    

    I don't really know what to believe in anymore..