mongodb/node-mongodb-native · error · MongoAPIError

SRV URI does not support directConnection

Error message

SRV URI does not support directConnection

What it means

Thrown (as MongoAPIError) inside the isSRV branch (connection_string.ts:472-474) when a mongodb+srv:// URI is combined with directConnection=true. SRV records resolve to multiple hosts and the driver uses topology discovery; directConnection pins to a single host and is semantically incompatible with SRV.

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 3366c21a63)

Solutions

  1. Remove directConnection from the SRV URI/options.
  2. If you truly need a direct single-host connection, use a standard mongodb:// URI with the explicit host:port instead of mongodb+srv://.
  3. Use directConnection only with mongodb:// URIs pointing at one host.

Example fix

// before
const c = new MongoClient('mongodb+srv://cluster.example/?directConnection=true');
// after
const c = new MongoClient('mongodb+srv://cluster.example/');
// or, for a direct single-host connection:
const c = new MongoClient('mongodb://node1.example:27017/?directConnection=true');
Defensive patterns

Strategy: validation

Validate before calling

if (uri.startsWith('mongodb+srv://') && (opts.directConnection || new URL(uri).searchParams.get('directConnection') === 'true')) {
  throw new Error('SRV URIs cannot use directConnection');
}

Prevention

When it happens

Trigger: URI like 'mongodb+srv://cluster.example/?directConnection=true' or new MongoClient('mongodb+srv://...', { directConnection: true }).

Common situations: Switching a single-host URI to SRV for managed Atlas clusters but keeping directConnection for a side task; misreading documentation that recommends directConnection for standalone instances.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/d972d7fa60f884c9.json. Report an issue: GitHub.