mongodb/node-mongodb-native · warning · MongoInvalidArgumentError

Invalid first parameter to count

Error message

Invalid first parameter to count

What it means

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.

Solutions

  1. Replace cursor.count() with collection.countDocuments(filter) or collection.estimatedDocumentCount().
  2. If you need skip/limit applied, pass them in CountOptions: cursor.count({ skip: 10, limit: 5 }).
  3. Delete the boolean argument entirely so the call is cursor.count() or cursor.count({}).

Example fix

// before
const n = await cursor.count(true);

// after
const n = await collection.countDocuments(filter, { skip: cursor.skip, limit: cursor.limit });
Defensive patterns

Strategy: validation

Validate before calling

function safeCount(cursor, options) {
  if (typeof options === 'boolean') {
    throw new TypeError('cursor.count no longer accepts a boolean; pass CountOptions or use collection.countDocuments');
  }
  return cursor.count(options);
}

Type guard

function isCountOptions(v) { return v == null || (typeof v === 'object' && typeof v !== 'boolean'); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at src/cursor/find_cursor.ts:156

      this.numReturned = this.numReturned + response.batchSize;

      return response;
    } finally {
      cleanup?.();
    }
  }

  /**
   * Get the count of documents for this cursor
   * @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead
   */
  async count(options?: CountOptions): Promise<number> {
    emitWarningOnce(
      'cursor.count is deprecated and will be removed in the next major version, please use `collection.estimatedDocumentCount` or `collection.countDocuments` instead '
    );
    if (typeof options === 'boolean') {
      throw new MongoInvalidArgumentError('Invalid first parameter to count');
    }
    return await executeOperation(
      this.client,
      new CountOperation(this.namespace, this.cursorFilter, {
        ...this.findOptions, // NOTE: order matters here, we may need to refine this
        ...this.cursorOptions,
        ...options
      })
    );
  }

  /** Execute the explain for the cursor */
  async explain(): Promise<Document>;
  async explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions): Promise<Document>;
  async explain(options: { timeoutMS?: number }): Promise<Document>;
  async explain(
    verbosity: ExplainVerbosityLike | ExplainCommandOptions,
    options: { timeoutMS?: number }

View on GitHub (pinned to dce7939f86)