I am trying to create MongoDB TTL indexe on a collection that contains documents with DateTimeOffset. But the date is persisted as an array of ticks and offset as such [ticks, offset]. For example :
TimeStamp:Array [
0:636803424000000000,
1:-360]
My model looks like the following:
public class Log
{
....
DateTimeOffset? TimeStamp{get; set;}
}
I am using MongoDb.Driver v2.7.2 and I have been trying to create index in such a way
....
var indexKeysDefinition = Builders<Log>.IndexKeys.Descending(l => l.TimeStamp);
var indexOptions = new CreateIndexOptions
{
ExpireAfter = TimeSpan..FromTicks(TimeSpan.TicksPerMinute),
Name = "LogsTimeStamPIndex",
Background = true
};
var model = new CreateIndexModel<Log>(indexKeysDefinition, indexOptions);
await _database.GetCollection<Log>("Log").Indexes.CreateOneAsync(model);
....
This is creating the index but documents are not expiring after 1 minute. How do I go about creating TTL for DateTimeOffset stored in such a way ? or What am I missing here ?
I guess this will help somebody in future. My team actually got in contact to MognoDb engineers and they have notified us that the date must be an ISO date to be used for TTL index. Obviously the [tick, offset] is not in a correct format. Thanks again Adam Harrison for pointing that out earlier.