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 collection() helper on Db no longer accepts a callback as its second argument; the callback-style API was removed in favor of promises. The guard at src/db.ts:326 checks typeof options === 'function' and throws MongoInvalidArgumentError synchronously. This catches code written against the pre-5.x callback API.

Solutions

  1. Drop the callback; collection() is synchronous and returns the Collection directly: const coll = db.collection('users').
  2. For async operations on the collection (find, insert), use await or .then() on the returned promise instead of a callback.
  3. Search the codebase for db.collection(.*function and remove all callback forms.

Example fix

// before
db.collection('users', (err, coll) => coll.find({}).toArray(cb));
// after
const coll = db.collection('users');
const docs = await coll.find({}).toArray();
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the second arg to db.collection is never a function.
function safeCollection(db, name, options) {
  if (typeof options === 'function') {
    throw new TypeError('collection() no longer accepts a callback; use promises');
  }
  return db.collection(name, options);
}

Type guard

function isCollectionOptions(v: unknown): v is CollectionOptions {
  return v == null || (typeof v === 'object' && typeof (v as any).readPreference !== 'function');
}

Try / catch

try {
  const coll = db.collection('users', opts);
} catch (e) {
  if (e instanceof MongoInvalidArgumentError && /callback form/.test(e.message)) {
    // migrate to promise form
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling db.collection('users', (err, coll) => {...}) or db.collection('users', opts, cb) where the last argument is a function. Any code migrated from mongo (v3/v4) without converting callbacks to await/Promise.

Common situations: Upgrading the driver from v4 to v5+ and leaving legacy callback code in place. Copying examples from old blog posts or Stack Overflow answers that predate the promise-only API.

Related errors


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

Appendix: 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 dce7939f86)