This error occurs when your MongoDB client fails to establish a connection within the configured timeout period. It typically happens due to network issues, firewall blocking, misconfigured timeout settings, or unreachable database servers.
MongoNetworkError: connection timeout indicates that your application failed to establish a TCP connection to the MongoDB server within the specified timeframe. The connection timeout determines the maximum amount of response time allowed for the client to connect to the MongoDB instance. This error can occur during initial connection attempts or when the driver tries to reconnect to the database after a connection drop.
First, confirm that your MongoDB instance is running and listening on the expected port. Test connectivity using telnet or nc:
# Test connectivity to MongoDB (port 27017 by default)
telnet mongodb-host.example.com 27017
# Or using nc
nc -zv mongodb-host.example.com 27017If the connection fails, ensure the MongoDB service is running and the hostname/port are correct.
The default connectTimeoutMS is often too low for networks with high latency. Increase it to at least 10000ms (10 seconds). For high-latency networks, use 30000ms or higher:
// Node.js/Mongoose
const uri = 'mongodb+srv://user:password@host/database?connectTimeoutMS=30000';
const client = new MongoClient(uri);
// Or using connection options
const client = new MongoClient(uri, {
connectTimeoutMS: 30000,
serverSelectionTimeoutMS: 30000
});# PyMongo
from pymongo import MongoClient
client = MongoClient(
'mongodb://host:27017/',
connectTimeoutMS=30000,
serverSelectionTimeoutMS=30000
)Ensure your firewall allows outbound connections to MongoDB on port 27017 (or your custom port). Check both local and cloud firewall settings:
# Check if port is open on Linux
sudo iptables -L -n | grep 27017
# Check ufw (Ubuntu)
sudo ufw status
# For MongoDB Atlas, whitelist your IP in Network Access section
# https://docs.atlas.mongodb.com/security/ip-access-list/For MongoDB Atlas specifically, add your application's IP address to the IP Access List in the MongoDB Atlas console.
Configure socketTimeoutMS to prevent long-running queries from blocking connections. Set it to 2-3 times the longest expected operation duration:
// Node.js example
const uri = 'mongodb+srv://user:password@host/database';
const client = new MongoClient(uri, {
connectTimeoutMS: 30000,
socketTimeoutMS: 45000,
serverSelectionTimeoutMS: 30000
});# PyMongo example
client = MongoClient(
'mongodb://host:27017/',
connectTimeoutMS=30000,
socketTimeoutMS=45000
)Test DNS resolution and ping connectivity to your MongoDB host:
# Test DNS resolution
nslookup mongodb-host.example.com
dig mongodb-host.example.com
# Test ping (may be disabled on some servers)
ping mongodb-host.example.com
# Trace network path
traceroute mongodb-host.example.comIf DNS resolution is slow, consider using IP addresses directly or configuring a faster DNS server.
In addition to connection timeouts, configure operation-level timeouts using maxTimeMS. This prevents individual queries from hanging:
// Node.js
const result = await collection.find(query).maxTimeMS(5000).toArray();# PyMongo
result = collection.find(query, maxTimeMS=5000)Set maxTimeMS to an appropriate value for your use case (typically 5000-30000ms depending on query complexity).
For serverless environments like AWS Lambda, connection pooling is problematic since each invocation may create new connection pools. Consider using MongoDB connection pooling libraries or increasing timeout values even more (60000ms+) for Lambda functions. For MongoDB Atlas clusters, ensure you are using the correct connection string with the proper authentication and are not missing required TLS settings. If using Mongoose with useUnifiedTopology, update to the latest driver version as older versions had known timeout issues. For load-balanced or multi-node replica sets, serverSelectionTimeoutMS should be set higher to allow the driver time to discover all replica set members. Consider monitoring your application logs and MongoDB server logs to identify whether timeouts occur consistently or intermittently, as this helps diagnose the root cause.
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