3

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

At the moment, I'm testing the TTL with expireAfterSeconds, and it doesn't work the way I'd like. 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.

I don't get what I'm doing wrong, any help ? Thanks

Ju Gaertner
  • 33
  • 1
  • 6

1 Answers1

3

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." See it here: https://docs.mongodb.com/v4.0/core/index-ttl/#timing-of-the-delete-operation

sanyassh
  • 8,100
  • 13
  • 36
  • 70
  • Oh ok, thanks. Does that mean the deletion will never happens after more than 60 seconds after the expiration I set ? I was worried the time could increase over the time, and 1 day would become 2 or something. – Ju Gaertner Feb 19 '19 at 09:33
  • I think that even if you have extremely large database and lots of running operation, the time to delete expired doc cant grow to days. Maybe minutes, I think. – sanyassh Feb 19 '19 at 09:38