简体   繁体   English

Pymongo和TTL错误的到期时间

[英]Pymongo and TTL wrong expiration time

I want to save data into MongoDB using pymongo, and it need to expire automatically after a month (maybe less) if noone removes it before (another script will perform a read + delete). 我想使用pymongo将数据保存到MongoDB中,并且如果没有人将其删除(另一个脚本将执行读取+删除),则该数据需要在一个月后(可能更少)自动过期。

At the moment, I'm testing the TTL with expireAfterSeconds , and it doesn't work the way I'd like. 目前,我正在使用expireAfterSeconds测试TTL,但它无法达到我想要的方式。 Here's my example : 这是我的例子:

client = MongoClient()
db = client.save_db
model = db.save_co
model.create_index("inserted", expireAfterSeconds = 120)
inserted_id = model.insert_one({"order_number":123456789, "inserted":datetime.datetime.utcnow()}).inserted_id

i = 1
while model.find_one(inserted_id) is not None:
    time.sleep(1)
    i += 1

print(i)
exit()

I think the printed value should be 120 , but it's actually 154 , or 160 , and sometimes 123 . 我认为打印值应该是120 ,但实际上是154160 ,有时是123

I don't get what I'm doing wrong, any help ? 我没有弄错我在做什么,有帮助吗? Thanks 谢谢

From the docs: "The TTL index does not guarantee that expired data will be deleted immediately upon expiration. There may be a delay between the time a document expires and the time that MongoDB removes the document from the database." 从文档中:“ TTL索引不保证过期的数据将在过期后立即删除。文档过期到MongoDB从数据库中删除文档之间可能会有延迟。” See it here: https://docs.mongodb.com/v4.0/core/index-ttl/#timing-of-the-delete-operation 在这里查看: https : //docs.mongodb.com/v4.0/core/index-ttl/#timing-of-the-delete-operation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM