我有以下代码。构造函数调用EnsureIndexes来确保在_expireAt字段上创建了TTL索引。然后,当通过调用AddOrUpdateItem方法插入文档时,它会在_expireAt字段中添加一个未来的日期。然而,这个日期过去了,文档永远不会过期。我做错了什么?
private void EnsureIndexes()
{
if (!_indexChecked)
{
// TTL index
var tsk =
MongoCollection.Indexes.CreateOneAsync(Builders<BsonDocument>.IndexKeys.Ascending("_expireAt"),
new CreateIndexOptions() { ExpireAfter = TimeSpan.FromSeconds(0) });
tsk.Wait();
_indexChecked = true;
}
}
public void AddOrUpdateItem(string key, TValue value, TimeSpan timeout)
{
var json = value.ToJson();
dynamic jObject = JObject.Parse(json);
jObject._expireAt = DateTime.UtcNow.Add(timeout);
json = jObject.ToString();
var replacementDocument = BsonSerializer.Deserialize<BsonDocument>(json);
var filter = new BsonDocument("_id", key);
var options = new UpdateOptions {IsUpsert = true};
var tsk = MongoCollection.ReplaceOneAsync(filter, replacementDocument, options);
try
{
tsk.Wait();
}
catch (AggregateException ex)
{
// TODO: Log
throw;
}
}
下面的命令返回的是获取Mongo集合上的索引的命令。
> db.Users.getIndices()
({"v":1,"key": {_id: 1},"name": "id"ns": "AuditDemo. "用户"},{"v":1,"key": {_expireAt: 1},name: _expireAt_1,"ns": "AuditDemo. "用户",expireAfterSeconds: 0}]>
在我的AddOrUpdateItem方法中,我首先将泛型类型序列化为json,以便能够为过期添加动态元素。然后我使用BsonSerializer将修改后的json反序列化为BsonDocument。在这一点上,是否BsonDocument将日期时间json字符串转换为BSon日期类型以便TTL索引工作?
findOne命令的结果
> db.Users.findOne({"_expireAt":{$exists: true}})
{_id: 0;UserGuid: {"Value": "054f6141-e655-41dd-a9d5-39382d3360ab"},"UserName": null,"FirstName": {"Value": "JORDAN"},"LastName": {"Value": "ACEVEDO"},"Email":{Value: JORDAN.ACEVEDO@fake.com}," __type ": "AuditDemo.ConsoleApplication.Models.Wss。用户信息,ConsoleTestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","_expireAt": "2015-05-31T10:23:15.8979321Z"}>
好了,我知道如何纠正这个日期问题了。我从JSON中得到的日期时间字符串。没有存储BSON日期对象。因此,我必须在反序列化的BsonDocument属性上调用BsonDateTime.create()方法,并强制其为BSON日期。当它被存储在正确的数据类型中时,TTL索引按预期工作。
DateTime expiresDate = new DateTime(DateTime.UtcNow.Ticks, DateTimeKind.Utc).Add(timeout);
var replacementDocument = BsonSerializer.Deserialize<BsonDocument>(json);
replacementDocument["_expireAt"] = BsonDateTime.Create(expiresDate);
var filter = new BsonDocument("_id", key);
var options = new UpdateOptions {IsUpsert = true};
var tsk = MongoCollection.ReplaceOneAsync(filter, replacementDocument, options);