A detailed example of a timed index in MongoDB

  • 2020-12-20 03:51:11
  • OfStack

There is an index in MongoDB called the TTL index (ES3en-ES4en-ES5en index, life-cycle index) that allows one timeout for each document. A document will be deleted if 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 need to be kept in the database for a limited time.

Specifying the expireAfterSeconds option in createIndex creates an TTL index:


//  The timeout period is 24 hours , The default is foreground run, can pass background:true Set to background mode 
db.user_session.createIndex({"updated":1},{expireAfterSeconds:60*60*24});

This creates an TTL index on the updated field. If the updated field 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 updated field of the document.


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

UTC save time The UTC save time is converted to GMT time when the result is searched, so you can see the difference between the saved time and the computer time by 8 hours (GMT+8).
db.getCollection('user_session').find({updated:{$gt: new Date("2019-07-12 14:00:00")}) can use new Date() to directly compare the time. new Date passed in the parameter is GMT time

]

To prevent active sessions from being deleted, update the value of the updated field to the current time when activity occurs on the session. As long as the time of updated is 24 hours from the current time. The corresponding document will be 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 once per minute, so you should not rely on seconds to keep the index alive. Moreover, the TTL index does not guarantee that expired data will be deleted immediately upon expiration. There may be a delay between when the document expires and when MongoDB removes the document from the database. The background task for deleting expired documents runs once every 60 seconds. As a result, the document may remain in the collection for the period between its expiration and the running of the background task.

Source in github. com/mongodb/mon...

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


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

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


{
  "expireAfterSeconds_old" : 60.0,
  "expireAfterSeconds_new" : 120.0,
  "ok" : 1.0
}

In a given set there can be multiple TTL index, you can be in created and updated fields create ttl index respectively, but cannot be used at the same time two fields based composite ttl index, also can't on the same one field and create TTL index, and create common index, but can be like "ordinary index" 1 sample used to sort and query optimization.

conclusion


Related articles: