Pymongo 和 TTL 错误的过期时间

Pymongo and TTL wrong expiration time

我想使用 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.

我不明白我做错了什么,有什么帮助吗?谢谢

来自文档:"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." 在此处查看:https://docs.mongodb.com/v4.0/core/index-ttl/#timing-of-the-delete-operation