mongodb/node-mongodb-native · error · MongoParseError

Proxy options cannot be specified multiple times in the conn

Error message

Proxy options cannot be specified multiple times in the connection string

What it means

Thrown when any of the proxy URI query parameters (proxyHost, proxyPort, proxyUsername, proxyPassword) appears more than once in the connection string (connection_string.ts:518-526). Unlike readPreferenceTags, the proxy options are scalar and must not be repeated; the driver refuses to guess which value wins.

Source

Thrown at src/connection_string.ts:523

    !mongoOptions.proxyHost &&
    (mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword)
  ) {
    throw new MongoParseError('Must specify proxyHost if other proxy options are passed');
  }

  if (
    (mongoOptions.proxyUsername && !mongoOptions.proxyPassword) ||
    (!mongoOptions.proxyUsername && mongoOptions.proxyPassword)
  ) {
    throw new MongoParseError('Can only specify both of proxy username/password or neither');
  }

  const proxyOptions = ['proxyHost', 'proxyPort', 'proxyUsername', 'proxyPassword'].map(
    key => urlOptions.get(key) ?? []
  );

  if (proxyOptions.some(options => options.length > 1)) {
    throw new MongoParseError(
      'Proxy options cannot be specified multiple times in the connection string'
    );
  }

  mongoOptions.mongoLoggerOptions = MongoLogger.resolveOptions(
    {
      MONGODB_LOG_COMMAND: process.env.MONGODB_LOG_COMMAND,
      MONGODB_LOG_TOPOLOGY: process.env.MONGODB_LOG_TOPOLOGY,
      MONGODB_LOG_SERVER_SELECTION: process.env.MONGODB_LOG_SERVER_SELECTION,
      MONGODB_LOG_CONNECTION: process.env.MONGODB_LOG_CONNECTION,
      MONGODB_LOG_CLIENT: process.env.MONGODB_LOG_CLIENT,
      MONGODB_LOG_ALL: process.env.MONGODB_LOG_ALL,
      MONGODB_LOG_MAX_DOCUMENT_LENGTH: process.env.MONGODB_LOG_MAX_DOCUMENT_LENGTH,
      MONGODB_LOG_PATH: process.env.MONGODB_LOG_PATH
    },
    {
      mongodbLogPath: mongoOptions.mongodbLogPath,
      mongodbLogComponentSeverities: mongoOptions.mongodbLogComponentSeverities,

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Ensure each proxy parameter appears at most once in the URI.
  2. Inspect how the URI is assembled (URLSearchParams.append vs set) and prefer set.
  3. Consolidate proxy configuration into the options object instead of the URI.

Example fix

// before
const uri = 'mongodb://host/?proxyHost=p1&proxyHost=p2';
// after
const uri = 'mongodb://host/?proxyHost=p1';
Defensive patterns

Strategy: validation

Validate before calling

const params = new URLSearchParams(new URL(uri).search);
for (const k of ['proxyHost', 'proxyPort', 'proxyUsername', 'proxyPassword']) {
  if (params.getAll(k).length > 1) throw new Error(`Proxy option ${k} appears more than once in the URI`);
}

Prevention

When it happens

Trigger: URI like 'mongodb://host/?proxyHost=p1&proxyHost=p2' or appending proxy params twice in a config builder.

Common situations: A config templating layer concatenating URI fragments that each include proxy options; misconfigured secret injection that appends rather than sets.

Related errors


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