@@ -161,6 +161,54 @@ specified in ``expireAfterSeconds``.
161
161
object is older than the number of seconds specified in
162
162
``expireAfterSeconds``.
163
163
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
+
164
212
Expire Documents at a Specific Clock Time
165
213
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166
214
0 commit comments