{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/find_cursor.ts#L138-L174","documentation":"Thrown by the deprecated FindCursor.count() (MongoInvalidArgumentError) when its first argument is a boolean. The old cursor.count(applySkipLimit) signature accepted a boolean; the current signature accepts only CountOptions. Passing true/false (often copy-pasted from old examples) is rejected.","triggerScenarios":"cursor.count(true) or cursor.count(false). Most commonly from pre-4.0 tutorials where count took a boolean applySkipLimit flag.","commonSituations":"Upgrading from driver v3 or earlier; copying old StackOverflow snippets; automated migrations that left boolean count() calls in place.","solutions":["Replace cursor.count() with collection.countDocuments(filter) or collection.estimatedDocumentCount()","If you must use count(), pass an options object (e.g. { limit, skip }) or no argument","Remove the boolean argument entirely"],"exampleFix":"// before\nconst n = await coll.find({}).limit(10).count(true);\n// after\nconst n = await coll.countDocuments({}, { limit: 10 });","handlingStrategy":"validation","validationCode":"// Before calling count(), ensure the arg is not a boolean\nfunction safeCount(cursor, opts) {\n  if (typeof opts === 'boolean') {\n    console.warn('cursor.count(boolean) is unsupported; migrating to countDocuments');\n    return cursor.client.db().collection('x').countDocuments({}, opts);\n  }\n  return cursor.count(opts);\n}","typeGuard":"const isCountOptions = (o) => o == null || typeof o === 'object';","tryCatchPattern":null,"preventionTips":["Migrate cursor.count() to collection.countDocuments() / estimatedDocumentCount()","Never pass a boolean to count(); pass CountOptions or nothing","Run a codemod to remove boolean count() calls during driver upgrades"],"tags":["find","count","deprecated","migration"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}