File tree Expand file tree Collapse file tree 4 files changed +67
-0
lines changed Expand file tree Collapse file tree 4 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,16 @@ public static IndexOptionsBuilder SetSparse(bool value)
101
101
return new IndexOptionsBuilder ( ) . SetSparse ( value ) ;
102
102
}
103
103
104
+ /// <summary>
105
+ /// Sets the time to live value.
106
+ /// </summary>
107
+ /// <param name="timeToLive">The time to live.</param>
108
+ /// <returns>The builder (so method calls can be chained).</returns>
109
+ public static IndexOptionsBuilder SetTimeToLive ( TimeSpan timeToLive )
110
+ {
111
+ return new IndexOptionsBuilder ( ) . SetTimeToLive ( timeToLive ) ;
112
+ }
113
+
104
114
/// <summary>
105
115
/// Sets whether the index enforces unique values.
106
116
/// </summary>
@@ -199,6 +209,17 @@ public IndexOptionsBuilder SetSparse(bool value)
199
209
return this ;
200
210
}
201
211
212
+ /// <summary>
213
+ /// Sets the time to live value.
214
+ /// </summary>
215
+ /// <param name="timeToLive">The time to live.</param>
216
+ /// <returns>The builder (so method calls can be chained).</returns>
217
+ public IndexOptionsBuilder SetTimeToLive ( TimeSpan timeToLive )
218
+ {
219
+ _document [ "expireAfterSeconds" ] = ( int ) timeToLive . TotalSeconds ;
220
+ return this ;
221
+ }
222
+
202
223
/// <summary>
203
224
/// Sets whether the index enforces unique values.
204
225
/// </summary>
Original file line number Diff line number Diff line change @@ -224,6 +224,25 @@ public BsonDocument RawDocument
224
224
get { return _document ; }
225
225
}
226
226
227
+ /// <summary>
228
+ /// Gets the time to live value (or TimeSpan.MaxValue if index doesn't have a time to live value).
229
+ /// </summary>
230
+ public TimeSpan TimeToLive
231
+ {
232
+ get
233
+ {
234
+ BsonValue value ;
235
+ if ( _document . TryGetValue ( "expireAfterSeconds" , out value ) )
236
+ {
237
+ return TimeSpan . FromSeconds ( value . ToInt32 ( ) ) ;
238
+ }
239
+ else
240
+ {
241
+ return TimeSpan . MaxValue ;
242
+ }
243
+ }
244
+ }
245
+
227
246
/// <summary>
228
247
/// Gets the version of the index.
229
248
/// </summary>
Original file line number Diff line number Diff line change @@ -68,6 +68,14 @@ public void TestSparse()
68
68
Assert . AreEqual ( expected , options . ToJson ( ) ) ;
69
69
}
70
70
71
+ [ Test ]
72
+ public void TestTimeToLive ( )
73
+ {
74
+ var options = IndexOptions . SetTimeToLive ( TimeSpan . FromHours ( 1 ) ) ;
75
+ string expected = "{ \" expireAfterSeconds\" : 3600 }" ;
76
+ Assert . AreEqual ( expected , options . ToJson ( ) ) ;
77
+ }
78
+
71
79
[ Test ]
72
80
public void TestUnique ( )
73
81
{
Original file line number Diff line number Diff line change @@ -276,6 +276,25 @@ public void TestDropIndex()
276
276
Assert . AreEqual ( 1 , _collection . GetIndexes ( ) . Count ( ) ) ;
277
277
}
278
278
279
+ [ Test ]
280
+ public void TestEnsureIndexTimeToLive ( )
281
+ {
282
+ if ( _server . BuildInfo . Version >= new Version ( 2 , 2 ) )
283
+ {
284
+ _collection . DropAllIndexes ( ) ;
285
+ Assert . AreEqual ( 1 , _collection . GetIndexes ( ) . Count ( ) ) ;
286
+
287
+ var keys = IndexKeys . Ascending ( "ts" ) ;
288
+ var options = IndexOptions . SetTimeToLive ( TimeSpan . FromHours ( 1 ) ) ;
289
+ _collection . EnsureIndex ( keys , options ) ;
290
+
291
+ var indexes = _collection . GetIndexes ( ) ;
292
+ Assert . AreEqual ( "_id_" , indexes [ 0 ] . Name ) ;
293
+ Assert . AreEqual ( "ts_1" , indexes [ 1 ] . Name ) ;
294
+ Assert . AreEqual ( TimeSpan . FromHours ( 1 ) , indexes [ 1 ] . TimeToLive ) ;
295
+ }
296
+ }
297
+
279
298
[ Test ]
280
299
public void TestExplain ( )
281
300
{
You can’t perform that action at this time.
0 commit comments