tailable() (Tailable Cursor)
Last updated: 2026-10-09
tailable() (Tailable Cursor)
tailable() marks a cursor as tailable, e.g. `db.logs.find().tailable()`. Instead of closing when data runs out, a tailable cursor keeps waiting for new documents — used to tail logs and watch for inserts before Change Streams existed. It requires a capped collection; in replica sets it pairs well with the awaitData option.
| Category | Cursor |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.find(<filter>).tailable({ awaitData: <bool> })
⚙️ Parameters
| awaitData | Optional. true blocks for new data instead of returning immediately. |
|---|
Returns:The cursor marked tailable (chainable).
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// tailable: keep reading new log entries
const cursor = db.logs.find()
.tailable({ awaitData: true })
while (cursor.hasNext()) {
print(cursor.next().msg)
}
Output:
login ok
order created
payment received
📌 Notes
- Capped collections only
- The cursor stays open at the tail, awaiting new data
- For new projects, prefer watch() Change Streams over tailable cursors
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat are tailable cursors for?
AThey keep reading new data from capped collections — real-time log tailing and monitoring streams — without closing at the end.
QWhat are tailable's limitations?
ACapped collections only; new projects should use watch() Change Streams, which are richer and work on normal collections.
QHow does tailable differ from watch?
Atailable() polls new data on capped collections; watch() uses the oplog Change Stream, supporting normal collections and more event types.