The TTL index (time to live index, which has a life-cycle indexing), which allows you to set a time-out for each document. A document is deleted after it reaches the pre-set aging level. This type of index is useful for caching problems, such as the preservation of a session.
When you create an index, you can create a TTL index by specifying the Expireafterseconds option, which is also part of the TTL collection.
Create a TTL index, set the time-out to 24 hours db.ttl.ensureIndex ({date:1},{expireafterseconds:24*60*60});
This creates a TTL index on the Date field. If the "date" of a document exists and its value is a date type, the document is deleted when the server time is expireafterseconds seconds later than the time of the document's "Date" field.
To prevent the active session from being deleted, you can update the Date field to the current time when there is an activity on the session. The document is deleted as long as "date" reaches 24 hours from the current time.
MongoDB cleans up the TTL index every minute, so you should not rely on the time in seconds to keep the index alive. You can use the Collmod command to change the value of Expireafterseconds:
Db.runcommand ({collmod: "ttl", index:{keypattern:{date:1}, expireafterseconds:36 00}})
You can have multiple TTL indexes on a given collection. The TTL index cannot be a composite index, but it can be used like a normal index to optimize sorting and querying.
This article is from the architect's path blog, so be sure to keep this source http://lizhuquan0769.blog.51cto.com/2591147/1761983
"MongoDB" TTL fixed Time collection