嵌套文档上的MongoDb TTL是否可行? [英] MongoDb TTL on nested document is possible?

查看:169
本文介绍了嵌套文档上的MongoDb TTL是否可行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在嵌套文档上使用TTL。

I want to know if it's possible to use TTL on nested documents.

我有帐户,里面我有会话会话需要在30分钟后过期。我已经设置了所有内容,但显然当我在 Account.Sessions.EndDateTime 上设置TTL索引时,它会删除整个帐户 。我可以确定它只删除会话吗?

I have Account and inside I have Sessions. Sessions need to expire in 30 minutes. I've set everything up but obviously when I set TTL index on Account.Sessions.EndDateTime it removes the whole Account. Can I make sure it removes only Session?

这就是它在数据库中的样子。注意当 EndDateTime 帐户而不仅仅是会话 c>将来。

This is what it looks like in database. Notice how it will delete whole Account and not only Session when EndDateTime will come.

{
    "_id" : ObjectId("53af273888dba003f429540b"),
    "Email" : "steve@s3te5ve.com",
    "PasswordHash" : "CZaBEQRbwWNgJBjyhks7gH0Z3v5ZvDkW29pryF0DEXyE8rIw0NA4x39+uQneArKaUv97sP1e+e22YT1glbqQsw==",
    "PasswordSalt" : "100000.Qx4D8uj7oDcWHRTLGRRTDwVkw2UcaM52XkDR9k2ga073Ow==",
    "Sessions" : [ 
        {
            "Token" : "da55cf0783c4249b26283948fcae6caa15df320ca456203045aea81cad691df8",
            "IpAddress" : "::1",
            "StartDateTime" : ISODate("2014-06-28T20:36:27.000Z"),
            "EndDateTime" : ISODate("2014-06-28T21:06:27.000Z")
        }
    ]
}

这是我创建索引的地方。

This is where I create said index.

if (!_db.Accounts.IndexExists("Sessions.EndDateTime"))
{
    _db.Accounts.CreateIndex(IndexKeys.Ascending("Sessions.EndDateTime"),
        IndexOptions.SetTimeToLive(new TimeSpan(0)));
}


推荐答案

这是目前无法实现的TTL指数。 Mongod将在指定的秒数或特定时钟时间后删除整个文档。

That is currently not possible with TTL index. Mongod will remove the whole document after a specified number of seconds or at a specific clock time.


TTL依赖mongod中的后台线程来读取索引中的日期类型
值并删除过期的来自集合的文档

我建议您将会话子文档存储在单独的集合中并添加该集合的TTL索引。

I would recommend that you store the session sub-document in a separate collection and add a TTL index on that collection.

如果您无法更改架构,则可以选择创建一个后台作业,该作业将每隔60秒从集合中删除嵌套文档。

If you can't change your schema, the alternative is to create a background job that will delete nested documents from your collection every 60 seconds.

这篇关于嵌套文档上的MongoDb TTL是否可行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆