1-
2-
31.. _index-feature-ttl:
42
53===========
@@ -25,26 +23,69 @@ TTL Indexes
2523
2624TTL indexes are special single-field indexes that MongoDB can use to
2725automatically remove documents from a collection after a certain amount
28- of time or at a specific clock time. Data expiration is useful for certain types of information
29- like machine generated event data, logs, and session information that
30- only need to persist in a database for a finite amount of time.
26+ of time or at a specific clock time. Data expiration is useful for
27+ certain types of information like machine generated event data, logs,
28+ and session information that only need to persist in a database for a
29+ finite amount of time.
3130
3231Create a TTL Index
3332------------------
3433
3534To create a TTL index, use the :method:`~db.collection.createIndex()`
3635method on a field whose value is either a :ref:`date
37- <document-bson-type-date>` or an array that contains :ref:`date values
38- <document-bson-type-date>`, and specify the ``expireAfterSeconds``
39- option with the desired TTL value in seconds.
36+ <document-bson-type-date>` or an array that contains date values, then specify the ``expireAfterSeconds`` option with the desired TTL value in
37+ seconds.
4038
4139For example, to create a TTL index on the ``lastModifiedDate`` field of
42- the ``eventlog`` collection, with a TTL value of ``3600`` seconds, use
40+ the ``eventlog`` collection with a TTL value of ``3600`` seconds, use
4341the following operation in :binary:`~bin.mongosh`:
4442
4543.. code-block:: javascript
4644
47- db.eventlog.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 3600 } )
45+ db.eventlog.createIndex( { "lastModifiedDate": 1 }, {
46+ expireAfterSeconds: 3600 } )
47+
48+ Starting in MongoDB 6.3, you can create partial TTL indexes on
49+ :ref:`time series collections <manual-timeseries-landing>`. These
50+ indexes use the collection ``timeField`` as the key field, and require a
51+ :ref:`partial filter expression <index-type-partial>` on the ``metaField``.
52+
53+ Time series collections include an optional ``expireAfterSeconds``
54+ field. If you do not set it, a TTL index with a
55+ ``partialFilterExpression`` lets you set an expiration period for
56+ documents that match the filter. If you do set ``expireAfterSeconds``,
57+ a partial TTL index lets you set a different, shorter expiration period
58+ for matching documents. You can only create a ``partialFilterExpression`` on the ``metaField``.
59+
60+ .. important::
61+
62+ If the ``expireAfterSeconds`` value of the collection is lower than
63+ the ``expireAfterSeconds`` of the partial TTL index, the collection
64+ deletes documents after the shorter time, so the TTL index has no effect.
65+
66+ This weather data time series collection deletes documents after 24 hours:
67+
68+ .. code-block:: javascript
69+
70+ db.createCollection(
71+ "weather24h",
72+ {
73+ timeseries: {
74+ timeField: "timestamp",
75+ metaField: "sensor",
76+ granularity: "hours"
77+ },
78+ expireAfterSeconds: 86400})
79+
80+ This TTL index deletes documents from the MongoDB NYC
81+ headquarters weather sensor after 1 hour, instead of 24 hours:
82+
83+ .. code-block:: javascript
84+
85+ db.eventlog.createIndex(
86+ { "timestamp": 1 },
87+ { partialFilterExpression: { "sensor": { $eq: "40.761873, -73.984287" } } },
88+ { expireAfterSeconds: 3600 } )
4889
4990.. _convert-non-ttl-single-field-index-into-ttl:
5091
@@ -118,12 +159,19 @@ Expiration of Data
118159
119160TTL indexes expire documents after the specified number of seconds has
120161passed since the indexed field value; i.e. the expiration threshold is
121- the indexed field value plus the specified number of seconds.
162+ the indexed field value plus the specified number of seconds.
122163
123164If the field is an array, and there are multiple date values in the
124165index, MongoDB uses *lowest* (i.e. earliest) date value in the array to
125166calculate the expiration threshold.
126167
168+ For time series collections, TTL indexes also remove a bucket of data
169+ when all documents inside it expire. This is equal to the upper
170+ timestamp limit of the bucket, plus the ``expireAfterSeconds`` value.
171+ For example, if a bucket covers data up until ``2023-03-27T18:29:59Z``
172+ and ``expireAfterSeconds`` is 300, the TTL index expires the
173+ bucket after ``2023-03-27T18:34:59Z``.
174+
127175If the indexed field in a document is not a
128176:ref:`date <document-bson-type-date>` or an array that holds one or
129177more date values, the document will not expire.
@@ -146,9 +194,9 @@ output of :method:`db.currentOp()` or in the data collected by the
146194Timing of the Delete Operation
147195``````````````````````````````
148196
149- MongoDB begins removing expired documents as soon as the index
150- finishes building on the :term:`primary`. For more information on the
151- index build process, see :ref:`index-operations`.
197+ MongoDB begins removing expired documents or time series buckets as soon
198+ as the index finishes building on the :term:`primary`. For more
199+ information on the index build process, see :ref:`index-operations`.
152200
153201.. include:: /includes/fact-ttl-collection-background-timing.rst
154202
@@ -177,10 +225,8 @@ Restrictions
177225- You cannot create a TTL index on a :ref:`capped collection
178226 <manual-capped-collection>`.
179227
180- - You cannot create a TTL index on a :doc:`time series collection
181- </core/timeseries-collections>`. Similar functionality is provided
182- through :ref:`automatic removal on time series collections
183- <manual-timeseries-automatic-removal>` instead.
228+ - You can only create TTL indexes for a :ref:`time series collection
229+ <manual-timeseries-landing>` on the collection ``timeField``.
184230
185231- You cannot use :method:`~db.collection.createIndex()` to change the
186232 value of ``expireAfterSeconds`` of an existing index. Instead use the
0 commit comments