Mongodb sets the TTL index to automatically clear data and expired data

  • 2020-11-20 06:18:57
  • OfStack

Mongodb is often used to store cached data or large, low-value data. For these types of data, the amount of data is often very large, and if not cleaned regularly, it will not only affect performance, but also waste a lot of system resources.

Mongodb can actually set the expiration time of data just like Redis1. TTL index is a special index in MongoDB, which can automatically delete documents after a certain time. Currently, TTL index can only be established on a single field.

When you create an TTL index for a field in the collection, there will be a single thread in the background, which will constantly query the value of the index (default is 60s1 times) to determine whether document has expired, and delete the document based on the load of the mongod instance. If the load is very high, the deletion may be delayed by a little while.

Create the TTL index method:

Same as normal index creation method 1, but with one more attribute

Example: In the log_events collection, the TTL index expires in 1 hour on the createTime field


db.log_events.createIndex( { "createTime": 1 },   --- The field names   
  { expireAfterSeconds: 60*60 } )         --- Expiration time (per second) 
[

The creteTime field type in the above example must be of type Date()

]

Modify the expireAfterSeconds attribute value of the TTL index:

Note: If you want to change the expiration time expireAfterSeconds, you can use the collMod method, or you can only use the dropIndex(),createIndex() method to rebuild the index, but this method is very painful in the billions of data


db.runCommand({ collMod: "log_events",     --- A collection of  
  index: { keyPattern: { createTime: 1 },   ---createTime As a TTL The field name of the index  
  expireAfterSeconds: 7200          --- The expiration time after modification ( seconds ) 
  }})

Although the above method can realize automatic expired deletion, if the business is very busy during the day, frequent deletion of data is bound to increase the load. Therefore, I want to delete expired data regularly at night (if the business volume is low at night).

The methods are as follows:

Add an expireTime field (to specify expiration time), set the expireAfterSeconds property value to 0,

Note: The createTime field above does not need the TTL index anymore. The time of the expireTime needs to be specified at insert time


db.log_events.createIndex( { "expireTime": 1 },   --- The field names  
  { expireAfterSeconds: 0 } )           --- Expiration time (per second) 

db.log_events.insert( { 
 "expireTime": new Date('Jan 22, 2019 23:00:00'), --- Specify an automatic delete time when you insert the document  
 "logEvent": 2, 
 "logMessage": "Success!"} ) 

Use spring-data-mongodb 2.0.9


Document document = new Document();
document.append("createTime",1);
IndexOptions indexOptions = new IndexOptions();
indexOptions.expireAfter(300L,TimeUnit.SECONDS);
this.mongoTemplate.getCollection("test").createIndex(document,indexOptions);

createTime is the time field

Use spring-ES70en-mongodb 1.7.0


BasicDBObject bson = new BasicDBObject();
      bson.append("createTime",1);
BasicDBObject options = new BasicDBObject();
options.append("expireAfterSeconds",7200);
      this.mongoTemplate.getCollection("test").createIndex(bson,options);

conclusion


Related articles: