Skip to content

Commit 52ab33e

Browse files
authored
DOCS-12534 Add Partial TTL Index Content (#5143) (#5249)
* DOCS-12534 Add Partial TTL Index Content * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 parent e443773 commit 52ab33e

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
@@ -158,6 +158,15 @@ by the filter expression
158158

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

161+
.. _partial-ttl-indexes:
162+
163+
Partial TTL Indexes
164+
~~~~~~~~~~~~~~~~~~~
165+
166+
Partial indexes can also be TTL indexes. Partial TTL indexes match the
167+
specified filter expression and expire only those documents. For details, see
168+
:ref:`partial-ttl-index-example`.
169+
161170
Restrictions
162171
------------
163172

source/tutorial/expire-data.txt

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

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

0 commit comments

Comments
 (0)