@@ -167,6 +167,54 @@ specified in ``expireAfterSeconds``.
167
167
object is older than the number of seconds specified in
168
168
``expireAfterSeconds``.
169
169
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
+
170
218
Expire Documents at a Specific Clock Time
171
219
-----------------------------------------
172
220
0 commit comments