Your application cannot reach the MongoDB Atlas cluster. This is usually caused by IP whitelisting restrictions, authentication issues, DNS problems, or network connectivity issues. Check your IP access list and connection string credentials first.
The MongooseServerSelectionError occurs when your Node.js/Mongoose application fails to establish a connection to any node in your MongoDB Atlas cluster. This error indicates a complete connection failure—the driver cannot discover or connect to any replica set members. The most common cause is that your current IP address is not whitelisted in MongoDB Atlas's IP Access List, which restricts which external addresses can connect to your cluster. Other causes include incorrect authentication credentials, DNS resolution failures, firewall blocks, or network routing issues in cloud environments.
Log in to MongoDB Atlas and navigate to Security > Network Access. You should see your current IP address in the whitelist. If not, click "Add IP Address" and select "Add Current IP Address" to automatically detect and add your IP. If you are deploying to a cloud service with dynamic IPs, add 0.0.0.0/0 to allow connections from anywhere (less secure but necessary for development).
In MongoDB Atlas, go to Clusters > Connect > Drivers. Copy the connection string provided. Ensure your .env file has the correct CONNECTION_STRING with your database user's username and password URL-encoded. Common issue: special characters in passwords must be URL-encoded (e.g., @ becomes %40).
Example correct format:
mongodb+srv://username:[email protected]/database?retryWrites=true&w=majority
Open a terminal and run:
nslookup your-cluster.mongodb.net
If this fails or returns no results, your DNS cannot resolve MongoDB's servers. Try using Google's DNS (8.8.8.8) or Cloudflare's DNS (1.1.1.1) instead of your ISP's default DNS. On Linux/Mac, edit /etc/resolv.conf; on Windows, use Control Panel > Network > DNS Settings.
Go to MongoDB Atlas > Clusters and verify your cluster shows a green status and is not paused. If it's paused, click "Resume". If the "Connect" button is disabled, the cluster is still provisioning—wait 5-10 minutes and refresh.
If deploying to AWS ECS, Heroku, Railway, or similar, ensure your outbound firewall/security group allows traffic on port 27017. In AWS, check that your security group has an outbound rule allowing all traffic (or specifically port 27017) to 0.0.0.0/0. For Heroku/Railway, these typically allow outbound by default.
In your Mongoose connection options, add these parameters to handle transient connection issues:
const mongoUri = process.env.MONGODB_CONNECTION_STRING;
await mongoose.connect(mongoUri, {
maxPoolSize: 10,
minPoolSize: 2,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
});
These settings give the driver more time and resources to establish connections.
Ensure you are using a recent version of Mongoose and the underlying MongoDB Node driver. Run:
npm update mongoose
Older versions may have connection stability issues or missing features.
For applications behind a NAT Gateway (AWS ECS/Fargate in private subnets), the NAT Gateway must be in a public subnet with a route to the Internet Gateway. Verify the route tables for private subnets include a route to the NAT Gateway (0.0.0.0/0). Consider using MongoDB Atlas VPC peering or AWS PrivateLink for production deployments to avoid traversing the public internet.
If you are using MongoDB Atlas with a free tier (M0 cluster), connection limits are lower (500 max connections). If you exceed this, you may see ServerSelectionError. Upgrade to M10 or higher, or implement connection pooling on the application side.
For advanced debugging, enable Mongoose debug logging:
mongoose.set("debug", true);
This will log all MongoDB driver operations and reveal exact timeout points.
DivergentArrayError: For your own good, using document.save() to update an array which was selected using an $elemMatch projection will not work
How to fix "DivergentArrayError: For your own good, using document.save() to update an array which was selected using an $elemMatch projection will not work" in MongoDB
MongoServerError: bad auth : authentication failed
How to fix "MongoServerError: bad auth : authentication failed" in MongoDB
CannotCreateIndex: Cannot create index
CannotCreateIndex: Cannot create index
StaleShardVersion: shard version mismatch
How to fix "StaleShardVersion: shard version mismatch" in MongoDB
MongoOperationTimeoutError: Operation timed out
How to fix "MongoOperationTimeoutError: Operation timed out" in MongoDB