Skip to content

Commit 8237f74

Browse files
authored
DOCS-12534 Add Partial TTL Index Content (#5143) (#5246)
* DOCS-12534 Add Partial TTL Index Content * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 parent 230511d commit 8237f74

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

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

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

source/tutorial/expire-data.txt

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

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

0 commit comments

Comments
 (0)