Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose connection throwing MongooseServerSelectionError

I am trying to connect to my mongodb Atlas via the mongoose driver and it keeps throwing a MongooseServerSelectionError, and i have checked my username and password for the database user and it is correct.

Here is my code:

server running on port 3000
we are connected: { MongooseServerSelectionError: connection timed out
    at new MongooseServerSelectionError (/home/ateyib/UCSD-classes/CSE 135/hw3/node_modules/mongoose/lib/error/serverSelection.js:22:11)
    at NativeConnection.Connection.openUri (/home/ateyib/UCSD-classes/CSE 135/hw3/node_modules/mongoose/lib/connection.js:808:32)
    at Mongoose.connect (/home/ateyib/UCSD-classes/CSE 135/hw3/node_modules/mongoose/lib/index.js:333:15)
    at Object.<anonymous> (/home/ateyib/UCSD-classes/CSE 135/hw3/server.js:17:4)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
    at internal/main/run_main_module.js:21:11
  message: 'connection timed out',
  name: 'MongooseServerSelectionError',
  reason:
   TopologyDescription {
     type: 'ReplicaSetNoPrimary',
     setName: 'Cluster0-shard-0',
     maxSetVersion: null,
     maxElectionId: null,
     servers:
      Map {
        'cluster0-shard-00-00-ebets.mongodb.net:27017' => [ServerDescription],
        'cluster0-shard-00-01-ebets.mongodb.net:27017' => [ServerDescription],
        'cluster0-shard-00-02-ebets.mongodb.net:27017' => [ServerDescription] },
     stale: false,
     compatible: true,
     compatibilityError: null,
     logicalSessionTimeoutMinutes: 30,
     heartbeatFrequencyMS: 10000,
     localThresholdMS: 15,
     commonWireVersion: 8 },
  [Symbol(mongoErrorContextSymbol)]: {} }
like image 419
Ateyib Abdulkadir Avatar asked Sep 06 '25 02:09

Ateyib Abdulkadir


2 Answers

It is something related to your ip address probably. Go to Network Access toolbar option click on EDIT button and then on ADD CURRENT IP ADDRESS and confirm. This worked for me.

like image 107
iNSIDE the mirror Avatar answered Sep 08 '25 20:09

iNSIDE the mirror


Using Mongo 3.6 and Mongoose 5.9.1 connecting to a replicaSet not on Atlas, setting useUnifiedTopology to true caused my issues. Removing this from my options made it work.

{ useUnifiedTopology: false } or don't set it at all and let it default to false

https://github.com/Automattic/mongoose/issues/8180

like image 28
Joshua Goldstein Avatar answered Sep 08 '25 21:09

Joshua Goldstein