{"record":{"id":"ab8b4a22774bc483","repo":"mongodb/node-mongodb-native","slug":"the-callback-form-of-this-helper-has-been-removed","errorCode":null,"errorMessage":"The callback form of this helper has been removed.","messagePattern":"The callback form of this helper has been removed\\.","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/db.ts","lineNumber":327,"sourceCode":"  /** Return the Admin db instance */\n  admin(): Admin {\n    return new Admin(this);\n  }\n\n  /**\n   * Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly.\n   *\n   * Collection namespace validation is performed server-side.\n   *\n   * @param name - the collection name we wish to access.\n   * @returns return the new Collection instance\n   */\n  collection<TSchema extends Document = Document>(\n    name: string,\n    options: CollectionOptions = {}\n  ): Collection<TSchema> {\n    if (typeof options === 'function') {\n      throw new MongoInvalidArgumentError('The callback form of this helper has been removed.');\n    }\n    return new Collection<TSchema>(this, name, resolveOptions(this, options));\n  }\n\n  /**\n   * Get all the db statistics.\n   *\n   * @param options - Optional settings for the command\n   */\n  async stats(options?: DbStatsOptions): Promise<Document> {\n    return await executeOperation(\n      this.client,\n      new DbStatsOperation(this, resolveOptions(this, options))\n    );\n  }\n\n  /**\n   * List all collections of this database with optional filter","sourceCodeStart":309,"sourceCodeEnd":345,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/db.ts#L309-L345","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Drop the callback; collection() is synchronous and returns the Collection directly: const coll = db.collection('users').","For async operations on the collection (find, insert), use await or .then() on the returned promise instead of a callback.","Search the codebase for db.collection(.*function and remove all callback forms."],"exampleFix":"// before\ndb.collection('users', (err, coll) => coll.find({}).toArray(cb));\n// after\nconst coll = db.collection('users');\nconst docs = await coll.find({}).toArray();","handlingStrategy":"validation","validationCode":"// Ensure the second arg to db.collection is never a function.\nfunction safeCollection(db, name, options) {\n  if (typeof options === 'function') {\n    throw new TypeError('collection() no longer accepts a callback; use promises');\n  }\n  return db.collection(name, options);\n}","typeGuard":"function isCollectionOptions(v: unknown): v is CollectionOptions {\n  return v == null || (typeof v === 'object' && typeof (v as any).readPreference !== 'function');\n}","tryCatchPattern":"try {\n  const coll = db.collection('users', opts);\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /callback form/.test(e.message)) {\n    // migrate to promise form\n  }\n  throw e;\n}","preventionTips":["Search the codebase for db.collection(.*function and remove all callback forms.","When upgrading driver majors, run the migration guide checklist for callback removal.","Lint against passing functions as the options argument."],"tags":["api-removal","migration","callback","synchronous"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}