mongodb/node-mongodb-native · error · MongoInvalidArgumentError

maxConnecting must be > 0 if specified

Error message

maxConnecting must be > 0 if specified

What it means

`maxConnecting` caps how many connections the pool opens concurrently and must be a positive integer. The transform (src/connection_string.ts:896-904) parses it as an unsigned int; if the result is `0` it throws a MongoInvalidArgumentError, because zero would block all connection establishment.

Source

Thrown at src/connection_string.ts:901

  },
  loadBalanced: {
    default: false,
    type: 'boolean'
  },
  localThresholdMS: {
    default: 15,
    type: 'uint'
  },
  maxAdaptiveRetries: {
    default: 2,
    type: 'uint'
  },
  maxConnecting: {
    default: 2,
    transform({ name, values: [value] }): number {
      const maxConnecting = getUIntFromOptions(name, value);
      if (maxConnecting === 0) {
        throw new MongoInvalidArgumentError('maxConnecting must be > 0 if specified');
      }
      return maxConnecting;
    }
  },
  maxIdleTimeMS: {
    default: 0,
    type: 'uint'
  },
  maxPoolSize: {
    default: 100,
    type: 'uint'
  },
  maxStalenessSeconds: {
    target: 'readPreference',
    transform({ name, options, values: [value] }) {
      const maxStalenessSeconds = getUIntFromOptions(name, value);
      if (options.readPreference) {
        return ReadPreference.fromOptions({

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Set maxConnecting to a positive integer (default is 2)
  2. Omit the option to use the default of 2
  3. Do not use 0 to mean unlimited

Example fix

// before
new MongoClient(uri, { maxConnecting: 0 });
// after
new MongoClient(uri, { maxConnecting: 4 });
Defensive patterns

Strategy: validation

Validate before calling

if (options.maxConnecting != null && !(Number(options.maxConnecting) > 0)) {
  throw new TypeError('maxConnecting must be a positive integer');
}

Type guard

function isValidMaxConnecting(v) {
  return Number.isInteger(Number(v)) && Number(v) > 0;
}

Prevention

When it happens

Trigger: `{ maxConnecting: 0 }`; `{ maxConnecting: '0' }`; a config template that defaults to 0.

Common situations: Setting it to 0 intending 'unlimited' (omit the option instead); environment config templating producing 0.

Related errors


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