Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 320
Dan Avatar asked Nov 14 '16 09:11

Dan


People also ask

Why is Mongoose timing out?

The buffering timeout is usually due to either registering models on a newly created connection but using mongoose. connect() : const mongoose = require('mongoose'); const schema = new mongoose.

Do I need to close Mongoose connection?

You should close a mongoose connection when a Node POSIX signal is happening. SIGINT process is triggered when Ctrl-C has been pressed on terminal or a server shutdown. Another possible scenario is to close a connection when a data streaming is done.

Why is my MongoDB not connecting?

Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

What is useUnifiedTopology in Mongoose?

serverSelectionTimeoutMS - With useUnifiedTopology , the MongoDB driver will try to find a server to send any given operation to, and keep retrying for serverSelectionTimeoutMS milliseconds. If not set, the MongoDB driver defaults to using 30000 (30 seconds).


1 Answers

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

like image 76
DBellavista Avatar answered Oct 01 '22 17:10

DBellavista