title: Express can't connect to mongodb replica set when new primary get elected
body: <p>I have a <code>mongodb replica set</code> with the following config:</p>
  
  <ul>
  <li>M1 primary</li>
  <li>M2 secondary</li>
  <li>M3 secondary</li>
  <li>M4 arbitrator</li>
  </ul>
  
  <p>Here is my Express code to connect to db (I use <code>mongoose</code> as the ODM):</p>
  
  <pre><code>const config = {
    db: `mongodb://${process.env.DB_ADDRESS_1}/${process.env.DB_NAME},
                   ${process.env.DB_ADDRESS_2}/${process.env.DB_NAME},
                   ${process.env.DB_ADDRESS_3}/${process.env.DB_NAME}`,
    dbOptions: {
      server: {
        socketOptions: {
          keepAlive: 1
        },
        poolSize: 5,
        readPreference: 'nearest'
      },
      replSet: {
        rs_name: process.env.REPLICA_SET,
        poolSize: 5,
        readPreference: 'nearest',
        socketOptions: {
          keepAlive: 1,
          connectTimeoutMS: 30000,
          socketTimeoutMS: 0
        }
      },
      db: {
        w: 1,
        numberOfRetries: 2
      }
    }
  }
  mongoose.connect(config.db, config.dbOptions, (error) =&gt; {
    if (error) {
      console.log('Error on connecting to th db: ', error)
      console.log('\x1b[31m', '*** PLEASE CONNECT TO DATABASE BEFORE RUN SERVER', '\x1b[0m')
      process.exit(1)
    }
    callback()
  })
  </code></pre>
  
  <p>The app works as expected.
  When M1 get down, either M2 or M3 get elected to be the <code>primary</code>, BUT my <code>express</code> app still CAN'T connect to the <code>replica set</code>.</p>
  
  <p>Is there any wrong in my config?</p>