When will a mongodb document expire after it is updated?

后端 未结 2 572
闹比i
闹比i 2021-02-05 23:10

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

相关标签:
2条回答
  • 2021-02-05 23:37

    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/

    0 讨论(0)
  • 2021-02-05 23:45

    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.

    0 讨论(0)
提交回复
热议问题