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