1
-
2
-
3
1
.. _index-feature-ttl:
4
2
5
3
===========
@@ -25,26 +23,69 @@ TTL Indexes
25
23
26
24
TTL indexes are special single-field indexes that MongoDB can use to
27
25
automatically 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.
31
30
32
31
Create a TTL Index
33
32
------------------
34
33
35
34
To create a TTL index, use the :method:`~db.collection.createIndex()`
36
35
method 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.
40
38
41
39
For 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
43
41
the following operation in :binary:`~bin.mongosh`:
44
42
45
43
.. code-block:: javascript
46
44
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 } )
48
89
49
90
.. _convert-non-ttl-single-field-index-into-ttl:
50
91
@@ -118,12 +159,19 @@ Expiration of Data
118
159
119
160
TTL indexes expire documents after the specified number of seconds has
120
161
passed 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.
122
163
123
164
If the field is an array, and there are multiple date values in the
124
165
index, MongoDB uses *lowest* (i.e. earliest) date value in the array to
125
166
calculate the expiration threshold.
126
167
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
+
127
175
If the indexed field in a document is not a
128
176
:ref:`date <document-bson-type-date>` or an array that holds one or
129
177
more date values, the document will not expire.
@@ -146,9 +194,9 @@ output of :method:`db.currentOp()` or in the data collected by the
146
194
Timing of the Delete Operation
147
195
``````````````````````````````
148
196
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`.
152
200
153
201
.. include:: /includes/fact-ttl-collection-background-timing.rst
154
202
@@ -177,10 +225,8 @@ Restrictions
177
225
- You cannot create a TTL index on a :ref:`capped collection
178
226
<manual-capped-collection>`.
179
227
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``.
184
230
185
231
- You cannot use :method:`~db.collection.createIndex()` to change the
186
232
value of ``expireAfterSeconds`` of an existing index. Instead use the
0 commit comments