{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/cursor/find_cursor.ts#L401-L437","documentation":"Thrown by FindCursor.sort when findOptions.tailable is true. Tailable cursors follow the natural order of documents in a capped collection as written by the server; applying a client-side sort is incompatible with that because it would require materializing and reordering the live stream. Subclass MongoTailableCursorError (extends MongoAPIError).","triggerScenarios":"collection.find({}, { tailable: true }).sort({ ts: -1 }); setting tailable via addCursorFlag('tailable', true) then calling .sort(); following the oplog with a sort applied.","commonSituations":"Building a change listener over a capped collection and mistakenly trying to order events; reusing a find-options builder that always sets sort; converting a normal query to tailable without removing sort.","solutions":["Remove the .sort() call on a tailable cursor; rely on insertion order.","If ordering is required, use a non-tailable query against the capped collection instead.","Switch to a Change Stream for ordered event processing rather than a tailable cursor."],"exampleFix":"// before\ncollection.find({}, { tailable: true }).sort({ ts: -1 });\n\n// after\ncollection.find({}, { tailable: true }); // natural order","handlingStrategy":"validation","validationCode":"function safeSort(cursor, sort, opts) {\n  if (opts?.tailable) throw new Error('Cannot sort a tailable cursor');\n  return cursor.sort(sort);\n}","typeGuard":"function isTailable(opts) { return Boolean(opts?.tailable); }","tryCatchPattern":null,"preventionTips":["Never combine sort with tailable cursors; rely on insertion order.","Use Change Streams when ordered event delivery is required."],"tags":["find","tailable","capped-collection","sort"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}