mongodb/node-mongodb-native · error · MongoAPIError

SRV URI does not support directConnection

Error message

SRV URI does not support directConnection

What it means

Thrown by MongoAPIError when the URI scheme is `mongodb+srv://` and the `directConnection=true` option is set. SRV records resolve to multiple hosts by design, while directConnection forces a single targeted host — the two are mutually exclusive. Check is at src/connection_string.ts:473 inside the `if (isSRV)` block.

Solutions

  1. Drop `directConnection=true` from the URI/options when using mongodb+srv://.
  2. If you truly must connect to one host directly, use the plain `mongodb://host:port` scheme with `directConnection=true` instead of the +srv scheme.

Example fix

// before
new MongoClient('mongodb+srv://cluster0.example.mongodb.net/?directConnection=true')

// after (single-host direct)
new MongoClient('mongodb://node1.example.mongodb.net:27017/?directConnection=true')
Defensive patterns

Strategy: validation

Validate before calling

function assertNotSrvPlusDirect(uri) {
  if (uri.startsWith('mongodb+srv://') && /directConnection=true/i.test(uri)) {
    throw new Error('mongodb+srv:// URIs cannot use directConnection.');
  }
}

Try / catch

try { new MongoClient(uri); } catch (e) { if (e instanceof MongoAPIError && /SRV URI does not support directConnection/.test(e.message)) { uri = uri.replace(/\??directConnection=true&?/i, ''); } else throw e; }

Prevention

When it happens

Trigger: `new MongoClient('mongodb+srv://cluster.example.mongodb.net/?directConnection=true')`, or passing `{ directConnection: true }` options with a +srv URI.

Common situations: Debugging connectivity to a single node and reusing the Atlas-style srv URI with `directConnection=true`; template config that sets directConnection globally applied to an Atlas URI.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/d972d7fa60f884c9. Report an issue: GitHub.

Appendix: source

Thrown at src/connection_string.ts:473

  if (mongoClient && mongoOptions.autoEncryption) {
    Encrypter.checkForMongoCrypt();
    mongoOptions.encrypter = new Encrypter(mongoClient, uri, options);
    mongoOptions.autoEncrypter = mongoOptions.encrypter.autoEncrypter;
  }

  // Potential SRV Overrides and SRV connection string validations

  mongoOptions.userSpecifiedAuthSource =
    objectOptions.has('authSource') || urlOptions.has('authSource');
  mongoOptions.userSpecifiedReplicaSet =
    objectOptions.has('replicaSet') || urlOptions.has('replicaSet');

  if (isSRV) {
    // SRV Record is resolved upon connecting
    mongoOptions.srvHost = hosts[0];

    if (mongoOptions.directConnection) {
      throw new MongoAPIError('SRV URI does not support directConnection');
    }

    if (mongoOptions.srvMaxHosts > 0 && typeof mongoOptions.replicaSet === 'string') {
      throw new MongoParseError('Cannot use srvMaxHosts option with replicaSet');
    }

    // SRV turns on TLS by default, but users can override and turn it off
    const noUserSpecifiedTLS = !objectOptions.has('tls') && !urlOptions.has('tls');
    const noUserSpecifiedSSL = !objectOptions.has('ssl') && !urlOptions.has('ssl');
    if (noUserSpecifiedTLS && noUserSpecifiedSSL) {
      mongoOptions.tls = true;
    }
  } else {
    const userSpecifiedSrvOptions =
      urlOptions.has('srvMaxHosts') ||
      objectOptions.has('srvMaxHosts') ||
      urlOptions.has('srvServiceName') ||
      objectOptions.has('srvServiceName');

View on GitHub (pinned to dce7939f86)