12

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.

When I update a document in this collection, which one of the following will happen?

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

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

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

d) None of the above

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

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

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
brice
  • 24,329
  • 7
  • 79
  • 95
  • Am testing the behaviour at the moment. – brice Sep 10 '12 at 12:31
  • 1
    The duration of the removal operations depend on the workload of your mongod instance that you are running , Could you please elaborate the situation that you are having – Aravind.HU Sep 10 '12 at 12:46
  • 1
    See updated answer. My current situation is pretty irrelevant though. I am aware that the removal will take time and the exact moment will depend on load. – brice Sep 10 '12 at 12:55
  • 1
    As per my understanding , Mongo DB follows a process to remove time expired docs , first it removes part of the index associated next the docs it has to remove , at last any other indexes it has to update when it removes docs – Aravind.HU Sep 10 '12 at 13:10

2 Answers2

18

The correct answer is c)

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.

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

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.

This feature is documented here: http://docs.mongodb.org/manual/tutorial/expire-data/

Philipp
  • 67,764
  • 9
  • 118
  • 153
0

If you don't want to rely on mongo demon process for expiring the collection, then better to create an additional createdOn field on collection and compare it with the current timestamp to decide whether to use that document or not.

Smart Coder
  • 1,435
  • 19
  • 19
  • This is unrelated to how MongoDB operates and requires external processing (which is outside the scope of the original question). – David Makogon Mar 10 '16 at 19:02