{"id":"3d8513b54a97264b","repo":"mongodb/node-mongodb-native","slug":"tailable-cursor-does-not-support-sorting","errorCode":null,"errorMessage":"Tailable cursor does not support sorting","messagePattern":"Tailable cursor does not support sorting","errorType":"exception","errorClass":"MongoTailableCursorError","httpStatus":null,"severity":"error","filePath":"src/cursor/find_cursor.ts","lineNumber":419,"sourceCode":"   * }});\n   * ```\n   */\n  project<T extends Document = Document>(value: Document): FindCursor<T> {\n    this.throwIfInitialized();\n    this.findOptions.projection = value;\n    return this as unknown as FindCursor<T>;\n  }\n\n  /**\n   * Sets the sort order of the cursor query.\n   *\n   * @param sort - The key or keys set for the sort.\n   * @param direction - The direction of the sorting (1 or -1).\n   */\n  sort(sort: Sort | string, direction?: SortDirection): this {\n    this.throwIfInitialized();\n    if (this.findOptions.tailable) {\n      throw new MongoTailableCursorError('Tailable cursor does not support sorting');\n    }\n\n    this.findOptions.sort = formatSort(sort, direction);\n    return this;\n  }\n\n  /**\n   * Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher)\n   *\n   * @remarks\n   * {@link https://www.mongodb.com/docs/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation}\n   */\n  allowDiskUse(allow = true): this {\n    this.throwIfInitialized();\n\n    if (!this.findOptions.sort) {\n      throw new MongoInvalidArgumentError('Option \"allowDiskUse\" requires a sort specification');\n    }","sourceCodeStart":401,"sourceCodeEnd":437,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cursor/find_cursor.ts#L401-L437","documentation":"Thrown as MongoTailableCursorError by FindCursor.sort() when findOptions.tailable is true. Tailable cursors follow a capped collection in natural insertion order; applying a sort would defeat the tailing semantics and is unsupported by the server, so the driver rejects it client-side.","triggerScenarios":"coll.find({}, { tailable: true }).sort({ ts: -1 }) or coll.find({}, { tailable: true, awaitData: true }).sort(...). Also when tailable is inferred for a change stream's underlying cursor in custom code.","commonSituations":"Building a tailing listener on the oplog/capped collection and adding sort(); converting a normal find to tailable without removing the sort call.","solutions":["Remove the .sort() call on tailable cursors — they iterate in natural order","If you need ordering, query a non-capped collection or read from the capped collection without tailable and sort the result set","Drop the tailable option if sorted results are required"],"exampleFix":"// before\ncoll.find({}, { tailable: true, awaitData: true }).sort({ ts: -1 });\n// after\ncoll.find({}, { tailable: true, awaitData: true });","handlingStrategy":"validation","validationCode":"function sortSafe(cursor, sort, direction) {\n  if (cursor.findOptions?.tailable) {\n    throw new Error('cannot sort a tailable cursor; remove tailable or sort');\n  }\n  return cursor.sort(sort, direction);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call .sort() on tailable cursors","Drop the tailable option if you need sorted results","For ordered reads from a capped collection, use a non-tailable query"],"tags":["find","tailable","sort","capped-collection"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}