Lifecycle and security · Engineering
TTL at collection, field, and document level
Expiry defaults, date-field TTL, per-document overrides, and why expired data is hidden before the sweeper runs.
Temporary data is everywhere: sessions, invites, password-reset tokens, caches, and short-lived events. The usual implementation is a cron job that deletes old rows, plus application logic to hide them before the job runs.
NonoDB supports document TTL at three levels: a collection default, a date-field policy, and a per-document override. Expiry metadata is written in the same transaction as the document and its indexes.
Strict visibility by default
Once _ttl_at <= now, the document is hidden from find, count, aggregation, SQL, search, lookups, cursors, and MongoDB-wire reads.
That is the important difference from a sweeper-only design. The sweeper reclaims space later, but reads stop seeing the document immediately. The old eventual-visibility behavior can be restored with NONODB_TTL_STRICT_READS=0; it is an emergency compatibility switch, not the default.
Collection default
Set a TTL in seconds at creation:
{ "op": "create_collection", "coll": "sessions", "ttl_seconds": 86400}Or change it later with set_collection_ttl. The default is insertion-based: each document expires ttl_seconds after it is inserted.
A changed default applies only to documents inserted after the change. It does not rewrite existing documents. Clear the default with null.
ttl_purge_history controls whether an expired document’s stored revisions are deleted instead of appending a delete tombstone.
Date-field TTL
If documents already contain a creation or retention timestamp, use date-field TTL:
{ "op": "set_collection_ttl_after", "coll": "sessions", "field": "createdAt", "ttl_seconds": 86400}A document expires at createdAt + ttl_seconds. Missing, null, or non-date values never expire.
Existing documents are not rewritten automatically. Backfill TTL metadata in bounded pages with backfill_collection_ttl, passing next_after until done is true. dry_run: true reports what would change without writing. Backfilled metadata is replicated, so replica reads match the primary.
The MongoDB wire listener accepts expireAfterSeconds on a single-field index and performs the same backfill. expireAfterSeconds: 0 is rejected because collection TTL must be positive. Dropping the translated index does not clear the collection TTL; use set_collection_ttl_after with another field or set_collection_ttl with null.
Date-field TTL is not supported for vault collections; use insertion-based expiry there.
Per-document TTL
The reserved top-level _ttl field overrides both modes:
{ "op": "insert", "coll": "sessions", "doc": {"_id": "s1", "user_id": 7, "_ttl": 300}}Rules:
_ttlis a non-negative integer number of seconds._ttl: 0ornulldisables expiry for that document.- If
_ttlis absent, the collection mode applies. - Negative, fractional, or non-numeric values return
invalid_ttl. _ttlis removed before storage and is not returned by reads.
The directive is not a document field. It is consumed by the write path.
Updates and history
With insertion-based TTL, a normal update preserves the existing expiry. With date-field TTL, updating the date field recomputes the expiry. An explicit update of _ttl changes the override.
Expiry uses the normal delete path. If history is enabled, the delete records a tombstone; purge_history deletes stored revisions instead. This keeps TTL consistent with the rest of the write semantics.
Maintenance
The maintenance worker submits expiry deletes to the single writer. It processes at most NONODB_TTL_SWEEP_LIMIT due documents every NONODB_TTL_SWEEP_INTERVAL seconds.
The sweeper is not what makes data invisible. It reclaims storage and removes the document. Strict reads hide it immediately.
Limits and interactions
- One TTL index per collection.
- The TTL index must be single-field and cannot be a geo index.
- Collection TTL must be positive.
- Vault collections support insertion-based TTL, not date-field TTL.
- TTL metadata is plaintext even for vault collections; the body is never decrypted by the sweeper.
- Snapshots and backups preserve TTL metadata; restore does not resurrect expired documents under strict reads.
A practical policy
- Use insertion-based TTL for sessions and tokens.
- Use date-field TTL when the event time is already stored.
- Use
_ttlfor exceptions, not as the primary policy. - Keep strict reads on unless a legacy client requires else.
- Monitor sweeper lag and disk usage.
- Remember that expiry is not compaction; the file may keep its high-water mark.
The TTL guide has Backfill, MongoDB compatibility, vault behavior, and metrics.