Skip to content

Commit 9ddbb96

Browse files
author
Dave Cuthbert
authored
DOCS-15586 Backport (#2204)
* DOCS-15586 expireAfterSeconds (#2136) * DOCS-15586 expireAfterSeconds * DOCS-15586 expireAfterSeconds * Review feedback * Review feedback * Upstream doc updated * Staging updates * Review feedback * DOCS-15586 BACKPORT * DOCS-15586 BACKPORT * DOCS-15586 BACKPORT * DOCS-15586 BACKPORT * DOCS-15586 BACKPORT * DOCS-15586 BACKPORT
1 parent 3640af7 commit 9ddbb96

File tree

9 files changed

+134
-19
lines changed

9 files changed

+134
-19
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Optional. Specifies a value, in seconds, as a time to live (:term:`TTL`)
2+
to control how long MongoDB retains documents in this collection. This
3+
option only applies to :term:`TTL` indexes. See :ref:`ttl-collections`
4+
for more information.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Starting in MongoDB 5.0.14 (and 6.0.2), the server will not use TTL
2+
indexes that have ``expireAfterSeconds`` set to ``NaN``.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
If you use TTL indexes created before MongoDB 5.0, or if you want to
2+
sync data created in MongDB 5.0 with a pre-5.0 installation, see
3+
:ref:`expireData-warning` to avoid misconfiguration issues.

source/reference/command/collMod.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ Index Options
8181
Modifying the index option ``expireAfterSeconds`` resets the
8282
:pipeline:`$indexStats` for the index.
8383

84+
.. include:: /includes/indexes/expireAfterSeconds-warning.rst
85+
8486
* - ``hidden``
8587

8688
- A boolean that determines whether the index is :doc:`hidden

source/reference/command/createIndexes.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,10 @@ Definition
310310

311311
- integer
312312

313-
- Optional. Specifies a value, in seconds, as a :term:`TTL` to control how long
314-
MongoDB retains documents in this collection. See
315-
:doc:`/tutorial/expire-data` for more information on this
316-
functionality. This applies only to :term:`TTL` indexes.
317-
313+
- .. include:: /includes/indexes/expireAfterSeconds-config-option.rst
314+
315+
.. include:: /includes/indexes/expireAfterSeconds-warning.rst
316+
318317
* - :ref:`hidden <cmd-createIndexes-hidden>`
319318

320319
- boolean

source/reference/method/db.collection.createIndex.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,10 @@ otherwise specified:
270270

271271
- integer
272272

273-
- Optional. Specifies a value, in seconds, as a :term:`TTL` to control how long
274-
MongoDB retains documents in this collection. See
275-
:doc:`/tutorial/expire-data` for more information on this
276-
functionality. This applies only to :term:`TTL` indexes.
277-
273+
- .. include:: /includes/indexes/expireAfterSeconds-config-option.rst
278274

275+
.. include:: /includes/indexes/expireAfterSeconds-warning.rst
276+
279277

280278
* - :ref:`hidden <method-createIndex-hidden>`
281279

source/reference/method/db.collection.createIndexes.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,10 @@ otherwise specified:
307307

308308
- integer
309309

310-
- Optional. Specifies a value, in seconds, as a :term:`TTL` to control how long
311-
MongoDB retains documents in this collection. See
312-
:doc:`/tutorial/expire-data` for more information on this
313-
functionality. This applies only to :term:`TTL` indexes.
314-
315-
310+
- .. include:: /includes/indexes/expireAfterSeconds-config-option.rst
311+
312+
.. include:: /includes/indexes/expireAfterSeconds-warning.rst
313+
316314
* - :ref:`hidden <method-createIndexes-hidden>`
317315

318316
- boolean

source/release-notes/5.0-compatibility.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,27 @@ Starting in MongoDB 5.0, the
147147
present on the :ref:`recipient shard <resharding-process-details>`
148148
when a :ref:`resharding operation <sharding-resharding>` is taking place.
149149

150+
.. _ttl_expireAfterSeconds_behavior:
151+
152+
TTL ``expireAfterSeconds`` Behavior When Set to ``NaN``
153+
-------------------------------------------------------
154+
155+
Starting in MongoDB 5.0, :ref:`TTL <ttl-collections>` indexes with
156+
``expireAfterSeconds`` set to ``NaN`` :ref:`experience a behavior change
157+
<expireData-warning>` compared to earlier versions.
158+
159+
The behavior change affects:
160+
161+
- direct upgrades
162+
- initial sync from earlier versions
163+
- :binary:`~bin.mongorestore` from earlier versions
164+
165+
Performing any of those actions causes an ``expireAfterSeconds`` value
166+
of ``NaN`` to be treated as an ``expireAfterSeconds`` of ``0``. As a
167+
result, documents may expire immediately.
168+
169+
.. include:: /includes/indexes/expireAfterSeconds-versions.rst
170+
150171
Shell Changes
151172
-------------
152173

source/tutorial/expire-data.txt

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ collection.
3131
Procedures
3232
----------
3333

34-
To create a :doc:`TTL index </core/index-ttl>`, use the
34+
To create a :ref:`TTL index <index-feature-ttl>`, use the
3535
:method:`db.collection.createIndex()` method with the
3636
``expireAfterSeconds`` option on a field whose value is either a
3737
:ref:`date <document-bson-type-date>` or an array that contains
@@ -47,7 +47,7 @@ You can modify the ``expireAfterSeconds`` of an existing TTL index
4747
using the :dbcommand:`collMod` command.
4848

4949
Expire Documents after a Specified Number of Seconds
50-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50+
----------------------------------------------------
5151

5252
To expire data after a specified number of seconds has passed since the
5353
indexed field, create a TTL index on a field that holds values of BSON
@@ -88,7 +88,7 @@ specified in ``expireAfterSeconds``.
8888
``expireAfterSeconds``.
8989

9090
Expire Documents at a Specific Clock Time
91-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91+
-----------------------------------------
9292

9393
To expire documents at a specific clock time, begin by creating a TTL
9494
index on a field that holds values of BSON date type or an array of
@@ -125,3 +125,91 @@ number of seconds specified in ``expireAfterSeconds``, i.e. ``0``
125125
seconds older in this case. As such, the data expires at the specified
126126
``expireAt`` value.
127127

128+
.. _expireData-warning:
129+
130+
Indexes Configured Using NaN
131+
----------------------------
132+
133+
.. warning::
134+
135+
Possible Data Loss
136+
137+
When a TTL index has ``expireAfterSeconds`` set to ``NaN``, upgrade,
138+
downgrade, and certain syncing operations can lead to unexpected
139+
behavior and possible data loss.
140+
141+
Do not set ``expireAfterSeconds`` to ``NaN`` in your TTL index
142+
configuration.
143+
144+
Prior to MongoDB 5.0, when a TTL index has ``expireAfterSeconds`` set to
145+
``NaN``, MongoDB logs an error and does not remove any records.
146+
147+
From MongoDB 5.0.0 - 5.0.13 (and 6.0.0 - 6.0.1), ``NaN`` is treated as
148+
``0``. If a TTL index is configured with ``expireAfterSeconds`` set to
149+
``NaN``, all TTL-indexed documents expire immediately.
150+
151+
.. include:: /includes/indexes/expireAfterSeconds-versions.rst
152+
153+
However, there are still some situations which may result in unexpected
154+
behavior. Documents may expire:
155+
156+
- During an initial sync to an earlier version from MongoDB 5.0.0 -
157+
5.0.13 (or 6.0.0 - 6.0.1).
158+
- When upgrading from an earlier version to MongoDB 5.0.0 - 5.0.13.
159+
- When restoring a collection from a pre-5.0 :binary:`~bin.mongodump`
160+
into a MongoDB 5.0.0 - 5.0.13 (or 6.0.0 - 6.0.1) instance.
161+
162+
To avoid problems, either drop or correct any misconfigured TTL indexes.
163+
164+
.. procedure::
165+
:style: normal
166+
167+
.. step:: Identify misconfigured indexes.
168+
169+
Run the following script in the :binary:`mongosh` shell. The
170+
script does not work in the legacy ``mongo`` shell.
171+
172+
.. code-block:: javascript
173+
174+
function getNaNIndexes() {
175+
const nan_index = [];
176+
177+
const dbs = db.adminCommand({ listDatabases: 1 }).databases;
178+
179+
dbs.forEach((d) => {
180+
const listCollCursor = db
181+
.getSiblingDB(d.name)
182+
.runCommand({ listCollections: 1 }).cursor;
183+
184+
const collDetails = {
185+
db: listCollCursor.ns.split(".$cmd")[0],
186+
colls: listCollCursor.firstBatch.map((c) => c.name),
187+
};
188+
189+
collDetails.colls.forEach((c) =>
190+
db
191+
.getSiblingDB(collDetails.db)
192+
.getCollection(c)
193+
.getIndexes()
194+
.forEach((entry) => {
195+
if (Object.is(entry.expireAfterSeconds, NaN)) {
196+
nan_index.push({ ns: `${collDetails.db}.${c}`, index: entry });
197+
}
198+
})
199+
);
200+
});
201+
202+
return nan_index;
203+
};
204+
205+
getNaNIndexes();
206+
207+
.. step:: Correct misconfigured indexes.
208+
209+
Use the :dbcommand:`collMod` command to update any misconfigured
210+
``expireAfterSeconds`` values that the script found.
211+
212+
As an alternative, you can :dbcommand:`drop <dropIndexes>` any
213+
misconfigured TTL indexes and recreate them later using the
214+
:dbcommand:`createIndexes` command.
215+

0 commit comments

Comments
 (0)