Documentations and Articles

Setup

Tips & Configuration

Use MongoDB Connect with DirectConnection

This configuration relate in configuration of MongoDB, when you set replicaset mode for your MongoDB, when you double-check the configuration member, you can see this at

For the configuration, you can use command with mongosh

rs.conf()

It will show your replicaset configuration in MongoDB, like this

{
  _id: <string>,
  version: <int>,
  term: <int>,
  protocolVersion: <number>,
  writeConcernMajorityJournalDefault: <boolean>,
  configsvr: <boolean>,
  members: [
    {
      _id: <int>,
      host: <string>,
      arbiterOnly: <boolean>,
      buildIndexes: <boolean>,
      hidden: <boolean>,
      priority: <number>,
      tags: <document>,
      secondaryDelaySecs: <int>,
      votes: <number>
    },
    ...
  ],
  settings: {
    chainingAllowed : <boolean>,
    heartbeatIntervalMillis : <int>,
    heartbeatTimeoutSecs: <int>,
    electionTimeoutMillis : <int>,
    catchUpTimeoutMillis : <int>,
    getLastErrorModes : <document>,
    getLastErrorDefaults : <document>,
    replicaSetId: <ObjectId>
  }
}

If It returns your host configuration as private ip address, you can encounter some problems about connection when this IP range same as IP range used for your network, that why sometime it will show connection timeout. So for fix this problem, you should connect directly into replicaset via directConnection=true parameter

mongosh 'mongodb://username:password@server-uri/db?directConnection=true'

Info

In some situations, you can bypass this configuration because it will add default into your connection string, like

  • TheΒ replicaSetΒ query parameter is present in the connection string.
  • The connection string uses theΒ mongodb+srv://Β connection string format.
  • The connection string contains a seed list with multiple hosts.
  • The connection string already contains aΒ directConnectionΒ parameter.