{"record":{"id":"02917a6b38b21773","repo":"mongodb/node-mongodb-native","slug":"invalid-first-parameter-to-count","errorCode":null,"errorMessage":"Invalid first parameter to count","messagePattern":"Invalid first parameter to count","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"warning","filePath":"src/cursor/find_cursor.ts","lineNumber":156,"sourceCode":"\n      this.numReturned = this.numReturned + response.batchSize;\n\n      return response;\n    } finally {\n      cleanup?.();\n    }\n  }\n\n  /**\n   * Get the count of documents for this cursor\n   * @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead\n   */\n  async count(options?: CountOptions): Promise<number> {\n    emitWarningOnce(\n      'cursor.count is deprecated and will be removed in the next major version, please use `collection.estimatedDocumentCount` or `collection.countDocuments` instead '\n    );\n    if (typeof options === 'boolean') {\n      throw new MongoInvalidArgumentError('Invalid first parameter to count');\n    }\n    return await executeOperation(\n      this.client,\n      new CountOperation(this.namespace, this.cursorFilter, {\n        ...this.findOptions, // NOTE: order matters here, we may need to refine this\n        ...this.cursorOptions,\n        ...options\n      })\n    );\n  }\n\n  /** Execute the explain for the cursor */\n  async explain(): Promise<Document>;\n  async explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions): Promise<Document>;\n  async explain(options: { timeoutMS?: number }): Promise<Document>;\n  async explain(\n    verbosity: ExplainVerbosityLike | ExplainCommandOptions,\n    options: { timeoutMS?: number }","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/find_cursor.ts#L138-L174","documentation":"Thrown by the deprecated FindCursor.count method when its first argument is a boolean. The legacy `count` API accepted an applySkipLimit boolean, but the current signature expects a CountOptions object; passing a boolean is a holdover from the old API and is explicitly rejected. Use collection.countDocuments or collection.estimatedDocumentCount instead.","triggerScenarios":"Calling cursor.count(true) or cursor.count(false) expecting it to apply skip/limit; legacy code migrated from the 2.x driver where count took a boolean second arg.","commonSituations":"Codebases migrating from the legacy node-bson/mongodb 2.x driver; copy-paste from outdated Stack Overflow answers; the deprecation warning being missed because emitWarningOnce only fires once.","solutions":["Replace cursor.count() with collection.countDocuments(filter) or collection.estimatedDocumentCount().","If you need skip/limit applied, pass them in CountOptions: cursor.count({ skip: 10, limit: 5 }).","Delete the boolean argument entirely so the call is cursor.count() or cursor.count({})."],"exampleFix":"// before\nconst n = await cursor.count(true);\n\n// after\nconst n = await collection.countDocuments(filter, { skip: cursor.skip, limit: cursor.limit });","handlingStrategy":"validation","validationCode":"function safeCount(cursor, options) {\n  if (typeof options === 'boolean') {\n    throw new TypeError('cursor.count no longer accepts a boolean; pass CountOptions or use collection.countDocuments');\n  }\n  return cursor.count(options);\n}","typeGuard":"function isCountOptions(v) { return v == null || (typeof v === 'object' && typeof v !== 'boolean'); }","tryCatchPattern":null,"preventionTips":["Migrate all cursor.count() calls to collection.countDocuments / estimatedDocumentCount.","Grep for cursor.count(true|false) in legacy code during driver upgrades."],"tags":["find","deprecated","count","options-validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}