20

I would like to know if it is possible to expire individual fields in a document rather than the entire document.

Having read the documentation on TTL Indexes and Expiring Data, it would appear that it is only possible to expire documents based on a field however I would like to confirm.

The background for this question is that I have a Statistics collection with fields for monthly, weekly, daily and hourly statistics in each document. I could break this up into 4 separate collections such that the documents in each collection could use a different TTL, however I believe this would also mean that I would need to correspondingly break my bulk updates into one bulk update per collection. I would like to keep the efficiency benefit of one single bulk operation if possible.

aerospatiale
  • 265
  • 2
  • 12
  • 1
    Why do you need different TTL's here? Naively, I think you can just have one. You could also just store the most fine-grained statistics and derive the other statistics using aggregation. It depends on your use case. – wdberkeley Sep 15 '14 at 16:13
  • So to be more specific about my use case, I am actually only interested in keeping the statistics for the current time period (at each granularity). My plan was to only update statistics for events whose timestamp is still relevant for each time period granularity and let the TTL expire the old values automatically. Would you expect a better performance from aggregation in this case? I'll look into this approach as well. – aerospatiale Sep 16 '14 at 00:52
  • 1
    Keep documents for each hour of statistics in a TTL index. Daily, weekly, and monthly statistics only need to be computed once per day, week, and month, respectively, so do that with the aggregation framework (I can help you with how to do that with more details about the stats/document structure, if needed). Hourly docs will expire once they past the 1 month old mark. Does that make sense? Also check out [time series modeling](http://blog.mongodb.org/post/65517193370/schema-design-for-time-series-data-in-mongodb). – wdberkeley Sep 16 '14 at 18:07

1 Answers1

5

It is impossible so far.

Your solution is ok if it is not a write heavy db. While if you want to reduce the writing frequency, there is another solution: Running a timer on certain server, which is used to query expired documents every 1 minute from slave mongo instance (or several minutes, depends on the accuracy you need). Then manually update these documents.

Mark_H
  • 770
  • 1
  • 6
  • 19