22

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 ?

5
  • the connection timeout at 'null' makes me suspect the proper port number isn't being passed to mongoose's connection.js module - have you considered this? Commented Nov 14, 2016 at 10:52
  • it connects fine runs fine for some time. So i think the connection works fine but this error comes up after 20-30 mins. Commented Nov 14, 2016 at 11:46
  • @Dan I have the same issue. If you could solve it, let me know how. Commented Nov 22, 2016 at 23:26
  • did you ever find a solution to this? Commented Dec 13, 2016 at 17:41
  • 1
    For more information please visit to: mongoosejs.com/docs/connections.html#options Commented Mar 2, 2017 at 8:13

3 Answers 3

22

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..

Sign up to request clarification or add additional context in comments.

1 Comment

Also see this: github.com/christkv/mongodb-core/issues/153 There is an issue when setting the timeout in the latest mongodb-core .
9

I realize that this is an old question, but the accepted answer now contains deprecated code. To set a connection timeout with Mongoose v5+, you now need to put all of the options at the top level of the options object, not nested like before:

mongoose.connect(uri, {
  useUnifiedTopology: true, // For Mongoose 5 only. Remove for Mongoose 6+
  serverSelectionTimeoutMS: 1000, // Defaults to 30000 (30 seconds)
})

5 Comments

Is 1000 enough to prevent timeouts? Is there any side effect if I set it to 30.000? @Mike
@GökhanPolat That depends on what you're trying to do. Set it to whatever you want the timeout to be. I just used 1000 as an example.
socketTimeoutMS is independent of useUnifiedTopology. It is not a deprecated/defunct option.
@ZachB It's been a while since I wrote this answer, but I believe this is what I was referring to. Now, options are declared at the top level, not nested.
@ZachB I've edited the answer for clarification.
1

RTFM ^^ - The only way to get up-to-date working option for a given product is to have a look at the relacted product version official documentation.

As today speaking, for the 8.3.x version here is a sample with link to the official documentation, that override default timeout (30 sec) to 3 seconds.

// set 3sec timeout to avoid waiting 30 seconds on misconfiguration:
// warn: this may produce flaky issues on some host with network latency

// doc: https://mongoosejs.com/docs/connections.html#serverselectiontimeoutms
const serverSelectionTimeoutMS = 3000;
// https://mongoosejs.com/docs/api/mongoose.html#Mongoose.prototype.connect()
const socketTimeoutMS = 3000;

const MONGOOSE_CONNECT_OPTIONS = {serverSelectionTimeoutMS, socketTimeoutMS};

// example in use
export const countDocumentsInCollection = async uri => {
    try {
        await mongoose.connect(uri, MONGOOSE_CONNECT_OPTIONS);
        return await myCollection.countDocuments();
    } finally {
        await mongoose.disconnect();
    }
}

Doc extract:

The serverSelectionTimeoutMS option is extremely important: it controls how long the MongoDB Node.js driver will attempt to retry any operation before erroring out. This includes initial connection, like await mongoose.connect(), as well as any operations that make requests to MongoDB, like save() or find().

and

[options.socketTimeoutMS=0] «Number» How long the MongoDB driver will wait before killing a socket due to inactivity after initial connection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.