Getting MongoServerSelectionError: Server selection timed out after 30000ms from localhost:5173

Hi All

I have a mongodb database wrapped inside Azure CosmosDB, I am using a node.js v16.17.1 application using the backend as azure CosmosDB. This application is deployed as a ContainerApp in Azure, there it is working fine while connecting to cosmos-db.

When I connect through localhost:5173:
It was working fine, but suddenly i am getting the below error from a particular day.

MongoServerSelectionError: Server selection timed out after 30000 ms
    at Timeout._onTimeout (node_modules/mongodb/lib/sdam/topology.js:291:38)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(1) {
      '' => ServerDescription {
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 7381815,
        lastWriteDate: 0,
        error: null,
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null

Even though the error show Server selection timed out after 30000 ms it does not take that much time too.

There are no permissions or network issue as I have allowed public network access to the db instance and it was always working.

even from mongsh <connectionstring> i am getting the same error.

Connection : is through my home wifi network which uses wpa2 personal. Before sometime back i was using wpa security. I am using a mac . My organization disallowed wpa security so i switched to wpa2 and ever since i notice this error when connecting from my localhost with my wifi network


Mobile hotspot:
Able to connect with my mobile-hotspot in the same machine to the same remote db without any errors.

Tried this will different versions of node.js and mongodb libraries including latest but the same error .

Can any one help here? Thanks

Hi @Bharath,

Welcome to the MongoDB Community forums :sparkles:

As I can see it might be due to your home wifi network. Please switch to some other network and try again. Also, it’s important to note that CosmosDB is not a MongoDB product so we cannot comment on what it can or cannot do, or its compatibility with a genuine MongoDB product.

I would suggest visiting the CosmosDB forums or reaching out to their support team if the problem persists.


As a complement to the previous answer by Kushagra, I suggest taking a look at this website - - for further information.