{"id":"dab3da28dafe6878","repo":"mongodb/node-mongodb-native","slug":"tailable-cursor-does-not-support-limit","errorCode":null,"errorMessage":"Tailable cursor does not support limit","messagePattern":"Tailable cursor does not support limit","errorType":"exception","errorClass":"MongoTailableCursorError","httpStatus":null,"severity":"error","filePath":"src/cursor/find_cursor.ts","lineNumber":468,"sourceCode":"   * Set the collation options for the cursor.\n   *\n   * @param value - The cursor collation options (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).\n   */\n  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) {","sourceCodeStart":450,"sourceCodeEnd":486,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/find_cursor.ts#L450-L486","documentation":"Thrown as MongoTailableCursorError by FindCursor.limit() when findOptions.tailable is true. Tailable cursors are open-ended streams over a capped collection; a limit would close the cursor after N docs, breaking the tailing contract, so the driver forbids it client-side.","triggerScenarios":"coll.find({}, { tailable: true }).limit(100) or coll.find({}, { tailable: true, awaitData: true }).limit(N).","commonSituations":"Trying to 'batch' tailing reads with a limit; reusing a find-options preset that includes limit on a tailable query.","solutions":["Remove .limit() on tailable cursors; track count yourself and close() when done","If you need a bounded read, query the capped collection without tailable and apply limit","Read N documents in your for-await loop then break and close()"],"exampleFix":"// before\ncoll.find({}, { tailable: true }).limit(10);\n// after\nconst c = coll.find({}, { tailable: true });\nlet i = 0;\nfor await (const doc of c) { if (++i >= 10) break; }\nawait c.close();","handlingStrategy":"validation","validationCode":"function limitSafe(cursor, n) {\n  if (cursor.findOptions?.tailable) {\n    throw new Error('cannot limit a tailable cursor; count manually and close()');\n  }\n  return cursor.limit(n);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call .limit() on tailable cursors","Track count yourself and break/close when reached","Use a non-tailable query for bounded reads"],"tags":["find","tailable","limit","capped-collection"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}