0

I want the collection i am inserting into mongodb to expire after a certain amount of time.

var db = mongo.GetDatabase("weatherdb");
using (mongo.RequestStart(db))
{
    var collection = db.GetCollection<BsonDocument>("weatherdatanew24")
        .EnsureIndex("Date" -> 1, "expireAfterSeconds" -> 120);
}

but the ensureINdex part is not working i think. Help please.

yaoxing
  • 4,003
  • 2
  • 21
  • 30

1 Answers1

0

Try this:

var db = mongo.GetDatabase("weatherdb");
using (mongo.RequestStart(db))
{
    var keys = IndexKeys.Ascending("Date");
    var options = IndexOptions.SetTimeToLive(TimeSpan.FromMinutes(2));
    var collection = db.GetCollection("weatherdatanew24").EnsureIndex(keys, options);
}
yaoxing
  • 4,003
  • 2
  • 21
  • 30