{"id":"1c78d60a0b9108b7","repo":"mongodb/node-mongodb-native","slug":"operation-limit-requires-an-integer","errorCode":null,"errorMessage":"Operation \"limit\" requires an integer","messagePattern":"Operation \"limit\" requires an integer","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/cursor/find_cursor.ts","lineNumber":472,"sourceCode":"  collation(value: CollationOptions): this {\n    this.throwIfInitialized();\n    this.findOptions.collation = value;\n    return this;\n  }\n\n  /**\n   * Set the limit for the cursor.\n   *\n   * @param value - The limit for the cursor query.\n   */\n  limit(value: number): this {\n    this.throwIfInitialized();\n    if (this.findOptions.tailable) {\n      throw new MongoTailableCursorError('Tailable cursor does not support limit');\n    }\n\n    if (typeof value !== 'number') {\n      throw new MongoInvalidArgumentError('Operation \"limit\" requires an integer');\n    }\n\n    this.findOptions.limit = value;\n    return this;\n  }\n\n  /**\n   * Set the skip for the cursor.\n   *\n   * @param value - The skip for the cursor query.\n   */\n  skip(value: number): this {\n    this.throwIfInitialized();\n    if (this.findOptions.tailable) {\n      throw new MongoTailableCursorError('Tailable cursor does not support skip');\n    }\n\n    if (typeof value !== 'number') {","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/find_cursor.ts#L454-L490","documentation":"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.","triggerScenarios":"cursor.limit('10'), cursor.limit(undefined), cursor.limit(10n) (BigInt), cursor.limit(config.limit) where config.limit is a string from JSON.","commonSituations":"Config/env values parsed as strings; BigInt arithmetic results; spreading any-typed option bags.","solutions":["Coerce: cursor.limit(Number(val)) and ensure it is a finite integer","Type your config so limit is number","Validate with Number.isInteger(Number(val)) before calling"],"exampleFix":"// before\ncursor.limit(env.LIMIT);\n// after\ncursor.limit(Number(env.LIMIT ?? 0));","handlingStrategy":"type-guard","validationCode":"function limitSafe(cursor, val) {\n  const n = Number(val);\n  if (!Number.isInteger(n)) throw new TypeError('limit must be an integer');\n  return cursor.limit(n);\n}","typeGuard":"const isIntegerNumber = (v) => typeof v === 'number' && Number.isInteger(v);","tryCatchPattern":null,"preventionTips":["Coerce string/env values with Number() and validate integer-ness","Type config objects so limit is number","Avoid passing BigInt results directly; convert with Number()"],"tags":["find","limit","validation","typescript"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}