Npm mongodb 3.6.3 useUnifiedTopology throws MongoServerSelectionError with replicaSet


  • When i updated to mongodb from 3.6.2 to 3.6.3 when i connect to the DB it throws error if useUnifiedTopology is set to true

  • Here is the snippet of the code (ommited username, password and db) using typescript

import { MongoClient } from 'mongodb';

async function initDB() {
  const mongoClient = new MongoClient(
    'mongodb://***:***@', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  const client = await mongoClient.connect()
  const data = await client.db().collection('providers').find().toArray()
  return data

initDB().then((result) => {
  console.log(`result`, result)
}).catch((err) => {
  console.log(`err`, err)

MongoServerSelectionError: getaddrinfo ENOTFOUND bawq-server
    at Timeout._onTimeout (/mnt/d/work/playground/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: 'rs1',
    maxSetVersion: 1,
    maxElectionId: 7fffffff0000000000000001,
    servers: Map(1) { 'bawq-server:27020' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: 9

But if useUnifiedTopology is set to false, it gives me warning , works fine and it gives me the results i expected

(node:2464) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring 
engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

Any help about this?

Edit: the problem only exists when using replica set (standalone) when i disable replica set option in config it works fine

1 Like

I just upgraded locally from 3.5.x to latest 3.6.3 and was also unable to connect to my local MongoDB instance. I got the same errors. I thought something was wrong with my dev server. At least it’s good to know that I’m not the only one. :wink: Back on 3.5 for now and it works.

I’m having the same issue when I use mongodb native driver 3.6.4. When I set useUnifiedTopology: false it doesn’t cause that selection error. I will downgrade to 3.5. Thanks @Nick for the tip!

I’ve resolved by changing the members’ hostname. See more: Mongoose v5.12.2: Connecting to MongoDB (