MongoDB Connection Error

MongoServerSelectionError: connection on NodeJs

MongoServerSelectionError: connection on NodeJs


If you start seeing the following error while using MongoDB using the connection URI (Database as-a service).

MongoServerSelectionError: connection <monitor> to 15.xxx.xxx.xxx:27017 closed
    at Timeout._onTimeout (/home/jsbisht/workspace/bubbles-india/server/node_modules/mongodb/lib/sdam/topology.js:312:38)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'cluster0-shard-00-00.xxx.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-01.xxx.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-02.xxx.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-xxx-shard-0',
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined
}

MongoTopologyClosedError: Topology is closed
    at Topology.close (/home/jsbisht/workspace/bubbles-india/server/node_modules/mongodb/lib/sdam/topology.js:247:42)
    at Object.callback (/home/jsbisht/workspace/bubbles-india/server/node_modules/mongodb/lib/sdam/topology.js:204:22)
    at Timeout._onTimeout (/home/jsbisht/workspace/bubbles-india/server/node_modules/mongodb/lib/sdam/topology.js:313:33)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)

Its happening because of the network policy in place for connecting to MongoDB. So, you need to log back into the MongoDB console. On the left navigation panel, choose network access:

mongodb-ip-whitelisting.png

Under the IP Address list table, either add a new IP address or edit the existing one. Choose my IP if you dont want to open it to broader audience.