{"id":"6080f561e3c0e7b9","repo":"mongodb/node-mongodb-native","slug":"operation-batchsize-requires-an-integer","errorCode":null,"errorMessage":"Operation \"batchSize\" requires an integer","messagePattern":"Operation \"batchSize\" requires an integer","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/cursor/abstract_cursor.ts","lineNumber":808,"sourceCode":"    }\n\n    this.cursorOptions.maxTimeMS = value;\n    return this;\n  }\n\n  /**\n   * Set the batch size for the cursor.\n   *\n   * @param value - The number of documents to return per batch. See {@link https://www.mongodb.com/docs/manual/reference/command/find/|find command documentation}.\n   */\n  batchSize(value: number): this {\n    this.throwIfInitialized();\n    if (this.cursorOptions.tailable) {\n      throw new MongoTailableCursorError('Tailable cursor does not support batchSize');\n    }\n\n    if (typeof value !== 'number') {\n      throw new MongoInvalidArgumentError('Operation \"batchSize\" requires an integer');\n    }\n\n    this.cursorOptions.batchSize = value;\n    return this;\n  }\n\n  /**\n   * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will\n   * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even\n   * if the resultant data has already been retrieved by this cursor.\n   */\n  rewind(): void {\n    if (this.timeoutContext && this.timeoutContext.owner !== this) {\n      throw new MongoAPIError(`Cannot rewind cursor that does not own its timeout context.`);\n    }\n    if (!this.initialized) {\n      return;\n    }","sourceCodeStart":790,"sourceCodeEnd":826,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/abstract_cursor.ts#L790-L826","documentation":"Thrown by cursor.batchSize(value) when value is not of type 'number' (on a non-tailable cursor). The driver requires a numeric integer for batchSize; strings, undefined, and BigInt are rejected. Note the message says 'requires an integer' but the runtime check is only typeof === 'number' — non-integer numbers are not caught here (the server may reject them).","triggerScenarios":"Calling batchSize('100'), batchSize(process.env.BATCH) with a string env var, batchSize(BigInt(100)), or batchSize(undefined). Fractional numbers like 1.5 pass the check but are invalid downstream.","commonSituations":"Env-var or JSON config feeding a string; missing value defaulting to undefined; copying a value that was typed loosely elsewhere.","solutions":["Coerce: batchSize(Number(value)).","Ensure integer-ness with Math.floor or guard: if (!Number.isInteger(v)) throw.","Type the option as number in your config schema."],"exampleFix":"// before\ncursor.batchSize(config.batch); // string from env\n// after\ncursor.batchSize(Number(config.batch));","handlingStrategy":"validation","validationCode":"if (typeof value !== 'number' || !Number.isInteger(value)) throw new TypeError('batchSize must be an integer');\ncursor.batchSize(value);","typeGuard":"function isInteger(v): v is number { return typeof v === 'number' && Number.isInteger(v); }","tryCatchPattern":null,"preventionTips":["Coerce string config values with Number() and Math.floor where needed.","Type batchSize as number in config schemas.","Validate integer-ness at your config boundary since the runtime check only verifies typeof."],"tags":["cursor","options","type-validation"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}