"MongoDB" TTL fixed Time collection

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.