{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/abstract_cursor.ts#L790-L826","documentation":"Thrown as a MongoInvalidArgumentError from batchSize() when the value argument fails `typeof value !== 'number'`. The message says 'requires an integer' but the implementation only checks for the number type at this line (it does not verify the value is an integer); batchSize is encoded as an int32 on the wire, so a non-number cannot be sent. The check runs after the tailable guard and only when the cursor is not initialized.","triggerScenarios":"cursor.batchSize('100'); cursor.batchSize(undefined); cursor.batchSize(100.5) passes the type check but is not actually an integer (the message is slightly stricter than the code).","commonSituations":"Passing a batchSize from JSON/env as a string; sharing an options object where batchSize was conditionally deleted to undefined; reading the error message and assuming integer validation that the code does not perform.","solutions":["Pass a number, preferably an integer: cursor.batchSize(100).","Parse config strings: cursor.batchSize(Number(process.env.BATCH_SIZE)).","If you rely on integer validation, add your own Number.isInteger(value) check before calling batchSize(), since the driver only checks the type here."],"exampleFix":"// before: string value\nconst size = process.env.BATCH_SIZE; // '100'\ncursor.batchSize(size);\n\n// after: parse and (optionally) validate integer\ncursor.batchSize(Number(process.env.BATCH_SIZE));","handlingStrategy":"type-guard","validationCode":"if (typeof value !== 'number' || !Number.isInteger(value)) {\n  throw new TypeError('batchSize must be an integer');\n}\ncursor.batchSize(value);","typeGuard":"function isInteger(v: unknown): v is number {\n  return typeof v === 'number' && Number.isInteger(v);\n}","tryCatchPattern":null,"preventionTips":["Parse string config values with Number() and validate with Number.isInteger.","Note the driver only checks the number type here; add your own integer check.","Avoid passing fractional batch sizes."],"tags":["cursor","batch-size","invalid-argument","typescript"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}