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() (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.
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 3366c21a63)
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
Example fix
// before
const n = await coll.find({}).limit(10).count(true);
// after
const n = await coll.countDocuments({}, { limit: 10 }); Defensive patterns
Strategy: validation
Validate before calling
// Before calling count(), ensure the arg is not a boolean
function safeCount(cursor, opts) {
if (typeof opts === 'boolean') {
console.warn('cursor.count(boolean) is unsupported; migrating to countDocuments');
return cursor.client.db().collection('x').countDocuments({}, opts);
}
return cursor.count(opts);
} Type guard
const isCountOptions = (o) => o == null || typeof o === 'object';
Prevention
- 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
When it happens
Trigger: cursor.count(true) or cursor.count(false). Most commonly from pre-4.0 tutorials where count took a boolean applySkipLimit flag.
Common situations: Upgrading from driver v3 or earlier; copying old StackOverflow snippets; automated migrations that left boolean count() calls in place.
Related errors
- ${optionWord} ${Array.from(unsupportedOptions).join(', ')} $
- Unable to make a writeConcern from fsync=${value}
- Unable to make a writeConcern from journal=${value}
- Cannot make WriteConcern from wtimeout
- Argument "iterator" must be a function
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/02917a6b38b21773.json.
Report an issue: GitHub.