Skip to content

Commit fef201f

Browse files
authored
DOCS-12534 Add Partial TTL Index Content (#5143)
* DOCS-12534 Add Partial TTL Index Content * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 parent 08c7b0e commit fef201f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

source/core/index-partial.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ by the filter expression
162162

163163
db.contacts.find( { name: "xyz", email: { $exists: false } } )
164164

165+
.. _partial-ttl-indexes:
166+
167+
Partial TTL Indexes
168+
~~~~~~~~~~~~~~~~~~~
169+
170+
Partial indexes can also be TTL indexes. Partial TTL indexes match the
171+
specified filter expression and expire only those documents. For details, see
172+
:ref:`partial-ttl-index-example`.
173+
165174
Restrictions
166175
------------
167176

source/tutorial/expire-data.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,54 @@ specified in ``expireAfterSeconds``.
167167
object is older than the number of seconds specified in
168168
``expireAfterSeconds``.
169169

170+
.. _partial-ttl-index-example:
171+
172+
Expire Documents with Filter Conditions
173+
---------------------------------------
174+
175+
To expire documents with specific filter expressions, you can create
176+
an index that is both a :ref:`partial <index-type-partial>`
177+
and a :ref:`TTL <index-feature-ttl>` index.
178+
179+
Create a partial TTL index:
180+
181+
.. code-block:: javascript
182+
:emphasize-lines: 5-6
183+
184+
db.foo.createIndex(
185+
{ F: 1 },
186+
{
187+
name: "Partial-TTL-Index",
188+
partialFilterExpression: { D : 1 },
189+
expireAfterSeconds: 10
190+
}
191+
)
192+
193+
Insert two documents, one of which matches the filter expression
194+
``{ D : 1 }`` of the ``partialFilterExpression``:
195+
196+
.. code-block:: javascript
197+
:emphasize-lines: 3
198+
199+
db.foo.insertMany( [
200+
{ "F" : ISODate("2019-03-07T20:59:18.428Z"), "D" : 3},
201+
{ "F" : ISODate("2019-03-07T20:59:18.428Z"), "D" : 1}
202+
] )
203+
204+
Wait for ten seconds then query the ``foo`` collection:
205+
206+
.. code-block:: javascript
207+
208+
db.foo.find({}, {_id: 0, F: 1, D: 1})
209+
210+
The document that matches the ``partialFilterExpression``
211+
of ``{ D : 1 }`` is deleted (expired). As a result, only
212+
one document remains in the ``foo`` collection:
213+
214+
.. code-block:: javascript
215+
216+
{ "F" : ISODate("2019-03-07T20:59:18.428Z"), "D" : 3}
217+
170218
Expire Documents at a Specific Clock Time
171219
-----------------------------------------
172220

0 commit comments

Comments
 (0)