mongodb/node-mongodb-native · critical · MongoUnexpectedServerResponseError

Invalid collection

Error message

Invalid collection ${this.collectionName}

What it means

Thrown by ValidateCollectionOperation.handleOk (src/operations/validate_collection.ts:44) when the validate response text matches /exception|corrupt/. The server itself reported an exception or corruption during validation, so the driver rethrows as MongoUnexpectedServerResponseError with the collection name. This indicates the collection's data is damaged on the server side.

Solutions

  1. Inspect the full validate output via db.runCommand({ validate: 'collname', full: true }) to identify corrupt documents.
  2. Restore the affected collection from backup or resync from a healthy replica-set member.
  3. Run db.repairDatabase() or contact MongoDB support for catastrophic corruption.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await db.admin().validateCollection(name);
} catch (e) {
  if (e instanceof MongoUnexpectedServerResponseError && /Invalid collection/.test(e.message)) {
    // page on-call; data corruption suspected
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling db.admin().validateCollection(name) where the server's validate output contains 'exception' or 'corrupt' (BSON/data corruption on disk).

Common situations: Disk failures, unclean shutdowns, storage-engine corruption, or running validate against a collection with known integrity problems.

Related errors


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

Appendix: source

Thrown at src/operations/validate_collection.ts:44

  override get commandName() {
    return 'validate' as const;
  }

  override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
    // Decorate command with extra options
    return {
      validate: this.collectionName,
      ...Object.fromEntries(Object.entries(this.options).filter(entry => entry[0] !== 'session'))
    };
  }

  override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
    const result = super.handleOk(response);
    if (result.result != null && typeof result.result !== 'string')
      throw new MongoUnexpectedServerResponseError('Error with validation data');
    if (result.result != null && result.result.match(/exception|corrupt/) != null)
      throw new MongoUnexpectedServerResponseError(`Invalid collection ${this.collectionName}`);
    if (result.valid != null && !result.valid)
      throw new MongoUnexpectedServerResponseError(`Invalid collection ${this.collectionName}`);

    return response;
  }
}

View on GitHub (pinned to dce7939f86)