mongodb/node-mongodb-native · error · MongoInvalidArgumentError
Operation "limit" requires an integer
Error message
Operation "limit" requires an integer
What it means
Thrown by FindCursor.limit when the value argument is not of type 'number'. The server requires an integer document count for limit, and the driver type-checks before storing it. Strings, booleans, undefined, and objects are rejected synchronously.
Solutions
- Coerce and validate finiteness: const n = Number(value); if (!Number.isFinite(n)) throw ...; cursor.limit(n).
- Pass limit through the find options instead: collection.find({}, { limit: 10 }).
- Guard the call site: if (typeof value === 'number') cursor.limit(value).
Example fix
// before cursor.limit(req.query.limit); // string from URL // after const limit = Number(req.query.limit); if (Number.isFinite(limit)) cursor.limit(limit);
Defensive patterns
Strategy: type-guard
Validate before calling
function safeLimit(cursor, value) {
const n = Number(value);
if (!Number.isFinite(n)) throw new TypeError('limit must be a finite number');
return cursor.limit(n);
} Type guard
function isFiniteInteger(v) { return typeof v === 'number' && Number.isFinite(v) && Number.isInteger(v); } Prevention
- Coerce pagination strings with Number() and validate with Number.isFinite.
- Pass limit through find options to centralize validation.
When it happens
Trigger: cursor.limit('10'); cursor.limit(parseInt(input)) where parseInt returned NaN; cursor.limit(undefinedVar); passing a value typed as number | undefined without a guard.
Common situations: Pagination params parsed from URL query strings (strings); Number/parseInt returning NaN; optional destructured config fields resolving to undefined.
Related errors
- Argument for maxAwaitTimeMS must be a number
- Argument for maxTimeMS must be a number
- Operation "skip" requires an integer
- Tailable cursor does not support limit
- AuthMechanismProperties must be an object
AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11).
Data as JSON: /api/errors/1c78d60a0b9108b7.
Report an issue: GitHub.
Appendix: source
Thrown at src/cursor/find_cursor.ts:472
collation(value: CollationOptions): this {
this.throwIfInitialized();
this.findOptions.collation = value;
return this;
}
/**
* Set the limit for the cursor.
*
* @param value - The limit for the cursor query.
*/
limit(value: number): this {
this.throwIfInitialized();
if (this.findOptions.tailable) {
throw new MongoTailableCursorError('Tailable cursor does not support limit');
}
if (typeof value !== 'number') {
throw new MongoInvalidArgumentError('Operation "limit" requires an integer');
}
this.findOptions.limit = value;
return this;
}
/**
* Set the skip for the cursor.
*
* @param value - The skip for the cursor query.
*/
skip(value: number): this {
this.throwIfInitialized();
if (this.findOptions.tailable) {
throw new MongoTailableCursorError('Tailable cursor does not support skip');
}
if (typeof value !== 'number') {View on GitHub (pinned to dce7939f86)