Pymongo和TTL错误的到期时间

问题描述 投票:1回答:1

我想使用pymongo将数据保存到MongoDB中,并且如果之前没有删除它(另一个脚本将执行读取+删除),它需要在一个月后(可能更少)自动过期。

目前,我正在用expireAfterSeconds测试TTL,它不能按照我喜欢的方式工作。这是我的例子:

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()

我认为打印的值应该是120,但它实际上是154,或160,有时123

我不知道我做错了什么,有什么帮助吗?谢谢

python mongodb pymongo ttl
1个回答
0
投票

来自文档:“TTL索引不保证过期数据将在到期后立即删除。文档到期时间与MongoDB从数据库中删除文档的时间之间可能会有延迟。”在这里看到:https://docs.mongodb.com/v4.0/core/index-ttl/#timing-of-the-delete-operation

© www.soinside.com 2019 - 2024. All rights reserved.