MongoDB has an index called a TTL index (a time-to-live index) that allows you to set a timeout for each document. A document is deleted when it reaches a preset aging level. Data expiration is useful for certain types of information, such as machine-generated event data, logs, and session information, that only needs to be kept in the database for a limited time.

Create a TTL index by specifying the expireAfterSeconds option in createIndex:

// The timeout period is 24 hours, the default is foreground, can be through background:trueDb. User_session. createIndex({db."updated":1},{expireAfterSeconds:60*60*24});
Copy the code

This creates a TTL index on the updated field. If a document’s updated field exists and its value is of date type, the document is deleted when the server time is expireAfterSeconds later than the document’s updated field.

db.getCollection('user_session').insert(
  {
    _id: NumberInt(1),
    "updated":new Date(),
     username:'lisi'});Copy the code

The UTC time used by mongodb to save the time will be converted to GMT time when the query result is generated. Db.getcollection (‘ user_Session ‘).find({updated:{$gt: New Date(“2019-07-12 14:00:00”)}}) you can use new Date() to compare the time, the new Date is passed in the GMT time

To prevent active sessions from being deleted, you can update the value of the updated field to the current time when activity occurs on the session. As long as the updated time is 24 hours from the current time. The corresponding document is deleted.

MongoDB’s TTL functionality relies on a background thread in MongoDB that reads date-type values in the index and removes expired documents from the collection. MongoDB cleans the TTL index every minute, so you should not rely on seconds to ensure that the index is alive. And TTL indexes do not guarantee that expired data will be deleted immediately upon expiration. There may be a delay between the time the document expires and the time MongoDB removes the document from the database. Background tasks to delete expired documents run every 60 seconds. Therefore, documents may remain in the collection during the period between the expiration of the document and the running of the background task.

The source code in github.com/mongodb/mon…

Mongodb does not support the createIndex command to reset the expiration time. You can only use the collMod command to change the value of expireAfterSeconds:

db.runCommand({collMod:"user_session",index: {name:"updated_1",expireAfterSeconds: 120}});
Copy the code

After the modification is successful, you will receive this message (the expiration time was 1 minute, now changed to 2 minutes).

{
    "expireAfterSeconds_old": 60.0."expireAfterSeconds_new": 120.0."ok": 1.0}Copy the code

You can create multiple TTL indexes on a given set. You can create A TTL index on both created and updated fields, but you cannot create a composite TTL index on both fields, and you cannot create a COMMON TTL index on the same field. But it can be used like a “normal index” to optimize sorting and queries.