简体   繁体   English

如何为存储为数组[ticks,offset]的DatetimeOffset创建MongoDB TTL索引?

[英]How to create MongoDB TTL Index for DatetimeOffset stored as an array [ticks,offset]?

I am trying to create MongoDB TTL indexe on a collection that contains documents with DateTimeOffset. 我正在尝试在包含带有DateTimeOffset的文档的集合上创建MongoDB TTL索引。 But the date is persisted as an array of ticks and offset as such [ticks, offset]. 但是日期以滴答和偏移量数组的形式保存,例如[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 我正在使用MongoDb.Driver v2.7.2,并且我一直在尝试以这种方式创建索引

....
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. 这是在创建索引,但是文档不会在1分钟后过期。 How do I go about creating TTL for DateTimeOffset stored in such a way ? 如何为以这种方式存储的DateTimeOffset创建TTL? 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. 我的团队实际上已经联系了MognoDb工程师,他们已经通知我们该日期必须是ISO日期才能用于TTL索引。 Obviously the [tick, offset] is not in a correct format. 显然[tick,offset]的格式不正确。 Thanks again Adam Harrison for pointing that out earlier. 再次感谢亚当·哈里森早些时候指出这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM