mongodb/node-mongodb-native · error · MongoInvalidArgumentError

The callback form of this helper has been removed.

Error message

The callback form of this helper has been removed.

What it means

The driver removed callback-style APIs in major version 5. db.collection() now requires options to be a plain object; the legacy signature db.collection(name, callback) is detected (options is a function) and rejected. Use the synchronous promise-based API instead — db.collection() is already non-async.

Source

Thrown at src/db.ts:327

  /** Return the Admin db instance */
  admin(): Admin {
    return new Admin(this);
  }

  /**
   * Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly.
   *
   * Collection namespace validation is performed server-side.
   *
   * @param name - the collection name we wish to access.
   * @returns return the new Collection instance
   */
  collection<TSchema extends Document = Document>(
    name: string,
    options: CollectionOptions = {}
  ): Collection<TSchema> {
    if (typeof options === 'function') {
      throw new MongoInvalidArgumentError('The callback form of this helper has been removed.');
    }
    return new Collection<TSchema>(this, name, resolveOptions(this, options));
  }

  /**
   * Get all the db statistics.
   *
   * @param options - Optional settings for the command
   */
  async stats(options?: DbStatsOptions): Promise<Document> {
    return await executeOperation(
      this.client,
      new DbStatsOperation(this, resolveOptions(this, options))
    );
  }

  /**
   * List all collections of this database with optional filter

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Remove the callback — db.collection() returns the Collection synchronously, no awaiting needed
  2. Pass options as an object, never a function
  3. Run the v4-to-v5 migration guide over every callback call site

Example fix

// before
db.collection('users', (err, coll) => { /* ... */ });
// after
const coll = db.collection('users');
Defensive patterns

Strategy: validation

Validate before calling

function getCollection(db, name, options) {
  if (typeof options === 'function') {
    throw new Error('Callback form removed in v5; use the synchronous return value');
  }
  return db.collection(name, options);
}

Type guard

function isPlainOptions(o: unknown): o is Record<string, unknown> {
  return o == null || (typeof o === 'object' && typeof (o as any).then !== 'function');
}

Prevention

When it happens

Trigger: Calling db.collection('users', cb) or passing a function as the second argument, typical of mongodb v4 (and earlier) callback code.

Common situations: Upgrading from mongodb v4 to v5+; copy-pasting legacy tutorial code; partial migrations that left callback call sites.

Related errors


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