在mongodb 3.0(.Net驱动程序2.0)中使用TTL未过期的文档
我有以下代码。构造函数调用EnsureIndex以确保在_expireAt字段上创建了TTL索引。然后,当通过调用AddOrUpdateItem方法插入文档时,它会将未来日期添加到_expireAt字段。但是,该日期已过,文档永不过期。我做错了什么在mongodb 3.0(.Net驱动程序2.0)中使用TTL未过期的文档,mongodb,mongodb-.net-driver,mongodb-csharp-2.0,Mongodb,Mongodb .net Driver,Mongodb Csharp 2.0,我有以下代码。构造函数调用EnsureIndex以确保在_expireAt字段上创建了TTL索引。然后,当通过调用AddOrUpdateItem方法插入文档时,它会将未来日期添加到_expireAt字段。但是,该日期已过,文档永不过期。我做错了什么 private void EnsureIndexes() { if (!_indexChecked) { // TTL index var tsk =
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;
}
}
[
{
“v”:1,
“关键”:{
“_id”:1
},
“名称”:“id”,
“ns”:“AuditDemo.Users”
},
{
“v”:1,
“关键”:{
“吃”:1
},
“名称”:“1”,
“ns”:“AuditDemo.Users”,
“expireAfterSeconds”:0
}
]
>
在我的AddOrUpdateItem方法中,我首先将泛型类型序列化为json,以便能够为expireAt添加动态元素。然后,我使用BsonSerializer将这个修改后的json反序列化为BsonDocument。此时,BsonDocument是否将datetime json字符串转换为BSon日期类型,以便TTL索引工作
findOne命令的结果
> db.Users.findOne({"_expireAt":{$exists: true}})
{
“_id”:“0”,
“UserGuid”:{
“值”:“054f6141-e655-41dd-a9d5-39382d3360ab”
},
“用户名”:空,
“名字”:{
“价值”:“约旦”
},
“姓氏”:{
“值”:“ACEVEDO”
},
“电子邮件”:{
“价值”:“约旦。ACEVEDO@fake.com"
},
“_-typ”:“AuditDemo.ConsoleApplication.Models.Wss.UserInfo,ConsoleTest
应用程序,版本=1.0.0.0,区域性=中性,PublicKeyToken=空“,
“_expireeat”:“2015-05-31T10:23:15.8979321Z”
}
>好的,我想出了如何纠正这个日期问题。我从JSON.Net获取的日期时间字符串没有存储在BSON date对象中。因此,我必须对反序列化的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);
DateTime expiresDate=new DateTime(DateTime.UtcNow.Ticks,DateTimeKind.Utc)。添加(超时);
var replacementDocument=BsonSerializer.Deserialize(json);
replacementDocument[“_expireAt”]=BsonDateTime.Create(expiresDate);
var filter=新的BsonDocument(“\u id”,key);
var options=newupdateoptions{IsUpsert=true};
var tsk=MongoCollection.ReplaceOneAsync(过滤器、替换文档、选项);
请使用编辑按钮将db.YourCollection.getIndexes()的输出添加到您的问题中。@MarkusWMahlberg:我用调用getIndexes的结果更新了原始帖子。该指数确实存在。但我怀疑这里可能发生的事情是将json文档反序列化为bson,BsonSerializer可能没有将json日期字符串转换为bson日期数据类型。如果是这样,那么如何存储过期的json文档。嗯,让我们检查一下。请添加db.Users.findOne({“\u expireAt”:{$exists:true}})
@MarkusWMahlberg的输出。补充。
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);