MongoServerSelectionError node js -16

MongoServerSelectionError: Server selection timed out after 30000 ms.

I am using node version 16.14.0 and mongoose version of 6.11.4. I am getting the same error repeatedly. I have checked the mongodb server also it is working fine.

Hey @Suresh_S1,

Welcome to the MongoDB Community!

  • Could you please confirm you are able to connect to the MongoDB server from mongosh or MongoDB Compass?
  • Also, kindly confirm that port 27017 is not blocked by clicking http://portquiz.net:27017.
  • Make sure the MongoDB connection string URI is correct in your Mongoose connect function. Verify hostname/port and username/password if used.
  • Additionally, try connecting from Mongoose without any options to see if that works. This helps rule out any issues with the connect options:
  mongoose.connect(uri)
  • You can also try increasing the connectTimeoutMS duration to see if that resolves the timeout issue:
  mongoose.connect(uri, {
    useNewUrlParser: true,
    connectTimeoutMS: 30000, // increase from default
  })

Please let us know if the timeout continues after verifying connectivity and adjusting the timeout settings. Feel free to provide more details on your setup and we’ll be glad to help you troubleshoot the issue!

Regards,
Kushagra

Even from mongosh and MongoDB Compass I am able to connect to local server. The port 27017 is not blocked. Even I tried connectionTimeoutMS and also increased the time. But still I am getting the same error.

MongoServerSelectionError: Server selection timed out after 30000 ms. In my project I use try catch block. In the try block first I use mongoose.connect to connect to the master DB and then parallelly I try to connect to my other data bases using mongoose.createConnection. In the old version of mongoose which is 5.10.3 the same is working fine. When I updated my mongoose to 6.11.4 the same above mentioned error is occuring. Please support the above issue.

Thanks & Regards,
Suresh

Hey @Suresh_S1,

To better assist with troubleshooting the issue, it would be very helpful if you could provide a code snippet that reproduces the problem along with the full text of the error message you are encountering. That will allow me to replicate the behavior on my end and troubleshoot further.

In the meantime, I tried the following script using Mongoose v6.11.4 which connects to a local MongoDB instance as expected:

const mongoose = require('mongoose');
const uri = 'mongodb://localhost:27017';

mongoose.connect(uri, { useUnifiedTopology: true, useNewUrlParser: true }).
  then(() => console.log('Connected')).
  catch(err => console.log('Connection Error', err.stack));

Please let me know if the above code snippet works for you, as it may help narrow down where the issue lies. Also, feel free to post any code and error logs you can share.

Look forward to hearing from you.

Regards,
Kushagra

The same code was working with earlier version of mongoose which is 5.10.3. When I have updated to the 6.11.4 the same was throwing an error. Refer the below image

Help me in this case if you find any solution

@Kushagra_Kesav Any solution for the above problem.