{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/find_cursor.ts#L454-L490","documentation":"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.","triggerScenarios":"cursor.limit('10'); cursor.limit(parseInt(input)) where parseInt returned NaN; cursor.limit(undefinedVar); passing a value typed as number | undefined without a guard.","commonSituations":"Pagination params parsed from URL query strings (strings); Number/parseInt returning NaN; optional destructured config fields resolving to undefined.","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)."],"exampleFix":"// before\ncursor.limit(req.query.limit); // string from URL\n\n// after\nconst limit = Number(req.query.limit);\nif (Number.isFinite(limit)) cursor.limit(limit);","handlingStrategy":"type-guard","validationCode":"function safeLimit(cursor, value) {\n  const n = Number(value);\n  if (!Number.isFinite(n)) throw new TypeError('limit must be a finite number');\n  return cursor.limit(n);\n}","typeGuard":"function isFiniteInteger(v) { return typeof v === 'number' && Number.isFinite(v) && Number.isInteger(v); }","tryCatchPattern":null,"preventionTips":["Coerce pagination strings with Number() and validate with Number.isFinite.","Pass limit through find options to centralize validation."],"tags":["find","limit","type-validation"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}