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() (MongoInvalidArgumentError) when value is not of type 'number'. limit must be an integer count; strings, undefined, BigInt, or objects are rejected before being sent to the server.

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 3366c21a63)

Solutions

  1. Coerce: cursor.limit(Number(val)) and ensure it is a finite integer
  2. Type your config so limit is number
  3. Validate with Number.isInteger(Number(val)) before calling

Example fix

// before
cursor.limit(env.LIMIT);
// after
cursor.limit(Number(env.LIMIT ?? 0));
Defensive patterns

Strategy: type-guard

Validate before calling

function limitSafe(cursor, val) {
  const n = Number(val);
  if (!Number.isInteger(n)) throw new TypeError('limit must be an integer');
  return cursor.limit(n);
}

Type guard

const isIntegerNumber = (v) => typeof v === 'number' && Number.isInteger(v);

Prevention

When it happens

Trigger: cursor.limit('10'), cursor.limit(undefined), cursor.limit(10n) (BigInt), cursor.limit(config.limit) where config.limit is a string from JSON.

Common situations: Config/env values parsed as strings; BigInt arithmetic results; spreading any-typed option bags.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/1c78d60a0b9108b7.json. Report an issue: GitHub.