mongodb文档更新后何时到期? [英] When will a mongodb document expire after it is updated?

查看:80
本文介绍了mongodb文档更新后何时到期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongodb中有一个文档集合,在日期类型索引上设置了expireAfterSeconds属性.

I have a collections of documents in mongodb, with the expireAfterSeconds property set on a date-type index.

为了争辩,文档被设置为在一小时后过期.

For the sake of argument, the documents are set to expire after one hour.

当我更新此集合中的文档时,会发生以下哪一项?

a).文档将在原始创建时间一小时后失效.

a) The document will expire one hour after the original creation time.

b).文档将在更新时间一小时后失效.

b) The document will expire one hour after the update time.

c),文档将在索引变量的时间一小时后过期,无论如何.

c) The document will expire one hour after the indexed variable's time, whatever that may be.

d)以上都不是

我认为它是 c ,但是找不到引用来确认.我对么?在哪里记录?

I think that it's c, but cannot find the reference to confirm it. Am I correct? Where is this documented?

[edit] :为了说明这一点,情况是我正在存储密码重置代码(应该过期),并且如果需要新的代码,我希望旧代码停止工作.但这并不是很相关,因为我可以通过简单地删除旧事务来确保始终遵守我想要的行为.这个问题不是关于我当前的问题,而是关于Mongo的行为.

[edit]: To clarify, the situation is that I'm storing password reset codes (that should expire.) and I want the old codes to stop working if a new code is requested. It's not very relevant though, since I can ensure the behaviour I want is always respected by simply deleting the old transaction. This question is not about my current problem, but about Mongo's behaviour.

推荐答案

正确的答案是 c)

expireAfterSeconds属性始终需要在包含BSON日期的字段上建立索引,因为此日期字段的内容用于选择要删除的条目.

The expireAfterSeconds property always requires an index on a field which contains a BSON date, because the content of this date field is used to select entries for removal.

当您要更新文档以重置生存时间时,还要将索引日期字段更新为当前时间.

When you want an update of a document to reset the time-to-live, also update the indexed date field to the current time.

如果您希望更新不影响TTL,请不要更新日期.

When you want an update to not affect the TTL, just don't update the date.

但是,请记住,expireAfterSeconds不能保证立即删除文档.删除操作由每分钟运行的后台作业完成.这项工作的优先级较低,当当前负载较高时,可以由MongoDB推迟.因此,对于您的用例而言,重要的是要准确地考虑到第二个过期时间,您应该在应用程序级别添加一个额外的检查.

However, keep in mind that expireAfterSeconds doesn't guarantee immediate deletion of the document. The deletions are done by a background job which runs every minute. This job is low-priority and can be postponed by MongoDB when the current load is high. So when it's important for your use-case that the expire times are respected accurately to the second, you should add an additional check on the application level.

此功能记录在这里: http://docs.mongodb.org/manual /tutorial/expire-data/

这篇关于mongodb文档更新后何时到期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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