Skip to content

Commit 5d80af4

Browse files
Docsp 28726 backport (#3145)
* Initital cleanup of deprecated version references * Typo fix * Version number cleanup * Rendering test * Added expiration behavior for time series collections * Added partial index limitations * Clarified partial TTL index behavior with time series collections * Internal review feedback * Removed note in restrictions topic aboout TTL indexes being unsupported * External PR feedback
1 parent aadae75 commit 5d80af4

File tree

9 files changed

+210
-268
lines changed

9 files changed

+210
-268
lines changed

source/core/index-partial.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ indexes only the documents with a ``rating`` field greater than 5.
3939
)
4040

4141
You can specify a ``partialFilterExpression`` option for all MongoDB
42-
:ref:`index types <index-types>`.
42+
:ref:`index types <index-types>`. When specifying a
43+
``partialFilterExpression`` for a TTL index on a time series collection,
44+
you can only filter on the collection ``metaField``.
4345

4446
.. seealso::
4547

source/core/index-ttl.txt

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
.. _index-feature-ttl:
42

53
===========
@@ -25,26 +23,69 @@ TTL Indexes
2523

2624
TTL indexes are special single-field indexes that MongoDB can use to
2725
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.
3130

3231
Create a TTL Index
3332
------------------
3433

3534
To create a TTL index, use the :method:`~db.collection.createIndex()`
3635
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.
4038

4139
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
4341
the 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

119160
TTL indexes expire documents after the specified number of seconds has
120161
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.
122163

123164
If the field is an array, and there are multiple date values in the
124165
index, MongoDB uses *lowest* (i.e. earliest) date value in the array to
125166
calculate 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+
127175
If the indexed field in a document is not a
128176
:ref:`date <document-bson-type-date>` or an array that holds one or
129177
more date values, the document will not expire.
@@ -146,9 +194,9 @@ output of :method:`db.currentOp()` or in the data collected by the
146194
Timing 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

source/core/timeseries/timeseries-limitations.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ These index types aren't supported:
9393
- :ref:`2d indexes <2d-index>`
9494
- :ref:`Unique indexes <index-type-unique>`
9595

96-
The :ref:`TTL <index-feature-ttl>` index property is not supported. For
97-
TTL deletion, use :ref:`expireAfterSeconds
98-
<db.createCollection.expireAfterSeconds>`.
99-
10096
You can only use the :ref:`multikey index <index-type-multikey>` type on
10197
the ``metaField``.
10298

source/includes/fact-ttl-collection-background-timing.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
The TTL index does not guarantee that expired data will be deleted
2-
immediately upon expiration. There may be a delay between the time that a
3-
document expires and the time that MongoDB removes the document from
1+
The TTL index does not guarantee that expired data is deleted
2+
immediately upon expiration. There may be a delay between the time that
3+
a document expires and the time that MongoDB removes the document from
44
the database.
55

66
The background task that removes expired documents runs *every 60

source/indexes.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ MongoDB creates a :ref:`unique index <index-type-unique>` on the
4040
:ref:`_id <document-id-field>` field during the creation of a
4141
collection. The ``_id`` index prevents clients from inserting two
4242
documents with the same value for the ``_id`` field. You cannot drop
43-
this index on the ``_id`` field.
43+
this index.
4444

4545
.. note::
4646

4747
In :term:`sharded clusters <sharded cluster>`, if you do *not* use
4848
the ``_id`` field as the :term:`shard key`, then your application
49-
**must** ensure the uniqueness of the values in the ``_id`` field
50-
to prevent errors. This is most-often done by using a standard
51-
auto-generated :term:`ObjectId`.
49+
**must** ensure the uniqueness of the values in the ``_id`` field.
50+
You can do this by using a field with an auto-generated :term:`ObjectId`.
5251

5352
Create an Index
5453
---------------
@@ -232,23 +231,24 @@ Sparse Indexes
232231
~~~~~~~~~~~~~~
233232

234233
The :ref:`sparse <index-type-sparse>` property of an index ensures
235-
that the index only contain entries for documents that have the indexed
234+
that the index only contains entries for documents that have the indexed
236235
field. The index skips documents that *do not* have the indexed field.
237236

238-
You can combine the sparse index option with the unique index option
239-
to prevent inserting documents that have duplicate values for the indexed
240-
field(s) and skip indexing documents that lack the indexed field(s).
237+
You can combine the ``sparse`` index option with the ``unique`` index
238+
option to prevent inserting documents that have duplicate values for the
239+
indexed field(s) and skip indexing documents that lack the indexed
240+
field(s).
241241

242242
.. _ttl-index:
243243

244244
TTL Indexes
245245
~~~~~~~~~~~
246246

247-
:ref:`TTL indexes <index-feature-ttl>` are special indexes that MongoDB
248-
can use to automatically remove documents from a collection after a
249-
certain amount of time. This is ideal for certain types of information
250-
like machine generated event data, logs, and session information that
251-
only need to persist in a database for a finite amount of time.
247+
:ref:`TTL indexes <index-feature-ttl>` automatically remove documents
248+
from a collection after a certain amount of time. This is ideal for
249+
certain types of information like machine generated event data, logs,
250+
and session information that only need to persist in a database for a
251+
finite amount of time.
252252

253253
See: :doc:`/tutorial/expire-data` for implementation instructions.
254254

0 commit comments

Comments
 (0)