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)
101101 return new IndexOptionsBuilder ( ) . SetSparse ( value ) ;
102102 }
103103
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+
104114 /// <summary>
105115 /// Sets whether the index enforces unique values.
106116 /// </summary>
@@ -199,6 +209,17 @@ public IndexOptionsBuilder SetSparse(bool value)
199209 return this ;
200210 }
201211
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+
202223 /// <summary>
203224 /// Sets whether the index enforces unique values.
204225 /// </summary>
Original file line number Diff line number Diff line change @@ -224,6 +224,25 @@ public BsonDocument RawDocument
224224 get { return _document ; }
225225 }
226226
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+
227246 /// <summary>
228247 /// Gets the version of the index.
229248 /// </summary>
Original file line number Diff line number Diff line change @@ -68,6 +68,14 @@ public void TestSparse()
6868 Assert . AreEqual ( expected , options . ToJson ( ) ) ;
6969 }
7070
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+
7179 [ Test ]
7280 public void TestUnique ( )
7381 {
Original file line number Diff line number Diff line change @@ -276,6 +276,25 @@ public void TestDropIndex()
276276 Assert . AreEqual ( 1 , _collection . GetIndexes ( ) . Count ( ) ) ;
277277 }
278278
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+
279298 [ Test ]
280299 public void TestExplain ( )
281300 {
You can’t perform that action at this time.
0 commit comments