Mongo的生存时间(TTL)索引对我不起作用

时间:2014-01-13 19:17:46

标签: mongodb

MongoDb 2.0.7。 这就是在c#:

中为存储对象MyObject声明日期的方式
public class MyObject
{
    [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
    public DateTime Created { get; set; }
    ...
}

这就是索引的创建方式:

Collection.EnsureIndex(new IndexKeysBuilder<MyObject>().Ascending(l => l.Created), IndexOptions.SetName("CreatedDateIndex").SetTimeToLive(new TimeSpan(0, 0, 60)));

MongoVUE工具确认使用CreatedDateIndex为集合MyObject创建了名为expireAfterSeconds = 60的索引。没有为Created字段声明的索引。

然而,集合中的项目永远不会被自动删除,我可以看到像这样的文档仍然有几天的旧项目:

{
  "_id" : new BinData(3, "OVdRaIodPUKz7cCuaxnFpA=="),
  "Created" : ISODate("2014-01-09T23:41:08.732Z"),
}

可能有什么问题?我只能猜测数据类型有问题,可能不是Bson Date类型,它甚至不会警告我,无论如何我甚至不知道如何验证它。

1 个答案:

答案 0 :(得分:4)

TTL索引从2.2开始是新的,而你使用的是2.0.7 ......