mongodb/node-mongodb-native · error · MongoOperationTimeoutError
Timed out during server selection
Error message
Timed out during server selection
What it means
Thrown when CSOT (Client-Side Operation Timeout, `timeoutMS`) is enabled and server selection cannot find a suitable server within the operation's remaining time budget; it wraps the underlying `MongoServerSelectionError` as the cause (topology.ts:659). It signals that no server matching the read preference / write requirements became available before the deadline.
Source
Thrown at src/sdam/topology.ts:660
if (
this.client.mongoLogger?.willLog(
MongoLoggableComponent.SERVER_SELECTION,
SeverityLevel.DEBUG
)
) {
this.client.mongoLogger?.debug(
MongoLoggableComponent.SERVER_SELECTION,
new ServerSelectionFailedEvent(
selector,
this.description,
timeoutError,
options.operationName
)
);
}
if (options.timeoutContext?.csotEnabled()) {
throw new MongoOperationTimeoutError('Timed out during server selection', {
cause: timeoutError
});
}
throw timeoutError;
}
// Other server selection error
throw error;
} finally {
abortListener?.[kDispose]();
if (!options.timeoutContext || options.timeoutContext.clearServerSelectionTimeout) {
timeout?.clear();
}
}
}
/**
* Update the internal TopologyDescription with a ServerDescription
*
* @param serverDescription - The server to update in the internal list of server descriptionsView on GitHub (pinned to 3366c21a63)
Solutions
- Verify cluster connectivity and health (mongosh, network, DNS) and wait out any failover.
- Ensure a server exists for your read preference (use 'primary'/'primaryPreferred' if only a primary is available).
- Raise `timeoutMS` / `serverSelectionTimeoutMS` to survive transient outages.
- Confirm the hosts in the connection string are reachable and not firewalled.
Example fix
// before
const client = new MongoClient(uri, { timeoutMS: 1000 });
// after
const client = new MongoClient(uri, { timeoutMS: 30000 }); Defensive patterns
Strategy: retry
Try / catch
try {
await coll.insertOne(doc, { timeoutMS: 5000 });
} catch (e) {
if (e instanceof MongoOperationTimeoutError && /server selection/i.test(e.message)) {
await new Promise(r => setTimeout(r, 1000));
return retry();
}
throw e;
} Prevention
- Set a timeoutMS/serverSelectionTimeoutMS large enough to survive failover (>= 10s).
- Use a read preference that matches available servers during incidents.
- Monitor cluster health and trigger failover-aware retries.
- Verify DNS/firewall reachability of all hosts before deploying.
When it happens
Trigger: An operation with `timeoutMS` set runs while the cluster is unreachable, has no primary (for writes), or has no secondaries (for secondary reads); network partition; all matching servers are deprioritized or unknown.
Common situations: Replica set with no elected primary during failover; secondary read preference but only a primary exists; firewall/DNS blocking the hosts; load-balanced mode with no ready service nodes; timeoutMS too small for a slow-starting cluster.
Related errors
- KMS request timed out
- Server roundtrip time is greater than the time remaining
- [Azure KMS] ${error.message}
- Server reported a timeout error
- Timed out during connection checkout
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/4fdb2fbfb25d235d.json.
Report an issue: GitHub.