mongodb/node-mongodb-native · error · MongoTailableCursorError
Tailable cursor does not support sorting
Error message
Tailable cursor does not support sorting
What it means
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.
Source
Thrown at src/cursor/find_cursor.ts:419
* }});
* ```
*/
project<T extends Document = Document>(value: Document): FindCursor<T> {
this.throwIfInitialized();
this.findOptions.projection = value;
return this as unknown as FindCursor<T>;
}
/**
* Sets the sort order of the cursor query.
*
* @param sort - The key or keys set for the sort.
* @param direction - The direction of the sorting (1 or -1).
*/
sort(sort: Sort | string, direction?: SortDirection): this {
this.throwIfInitialized();
if (this.findOptions.tailable) {
throw new MongoTailableCursorError('Tailable cursor does not support sorting');
}
this.findOptions.sort = formatSort(sort, direction);
return this;
}
/**
* Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher)
*
* @remarks
* {@link https://www.mongodb.com/docs/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation}
*/
allowDiskUse(allow = true): this {
this.throwIfInitialized();
if (!this.findOptions.sort) {
throw new MongoInvalidArgumentError('Option "allowDiskUse" requires a sort specification');
}View on GitHub (pinned to 3366c21a63)
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
Example fix
// before
coll.find({}, { tailable: true, awaitData: true }).sort({ ts: -1 });
// after
coll.find({}, { tailable: true, awaitData: true }); Defensive patterns
Strategy: validation
Validate before calling
function sortSafe(cursor, sort, direction) {
if (cursor.findOptions?.tailable) {
throw new Error('cannot sort a tailable cursor; remove tailable or sort');
}
return cursor.sort(sort, direction);
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: Building a tailing listener on the oplog/capped collection and adding sort(); converting a normal find to tailable without removing the sort call.
Related errors
- Tailable cursor does not support limit
- Tailable cursor does not support skip
- Argument for maxAwaitTimeMS must be a number
- Option "allowDiskUse" requires a sort specification
- Invalid sort direction: ${JSON.stringify(direction)}
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/3d8513b54a97264b.json.
Report an issue: GitHub.